0

So what I'm doing is creating a directory with a name from the file , then creating a text file in it.

My directory structure is like this where I am trying to create the file:

/users/username/www/practise

and the code is:

$path = /home/username/www/practise/

$counter_file = "/home/username/www/practise/counter.txt";

if (file_exists($counter_file)) {

line 2 : if(!($fp = fopen($counter_file, "r")))

die("can't open the file");

          $counter = (int) fread($fp, 20);
          fclose($fp);

          $counter = $counter + 1 ;
      $path .= $counter

          mkdir($path , 0777);
          $path .= content.txt;

          if (!($fp = fopen($path, "w"))) 
          {
              die("cannot open the file");

          }
          fwrite($fp, "{$content}");
  // content is the variable that has value equal to "file is created"
          fclose($fp);

          if(!($fp = fopen($counter_file, "w")))
              die("cann't open the file");

          fwrite($fp, $counter);
          fclose($fp);            // and store the counter value in the file

          if(!($fp = fopen("$counter_file", "w")))
              die("cann't open the file");

          fwrite($fp, $counter);
          fclose($fp);            

}

The error which I am getting is:

Warning: fopen(counter.txt): failed to open stream: Permission denied on line 2

can't open the file

Deepu
  • 131
  • 6
  • Try `/practise/counter.txt`. – putvande Mar 01 '14 at 12:36
  • still the same error @putvande – Deepu Mar 01 '14 at 12:39
  • do you have write permissions? – ɹɐqʞɐ zoɹǝɟ Mar 01 '14 at 12:39
  • What permissions are on the `practise` directory – putvande Mar 01 '14 at 12:40
  • the permissions on the practise directory is 777 – Deepu Mar 01 '14 at 12:41
  • what OS? normally `/home/username` folder is not accessible for others. If that is not the case, try checking if `SeLinux` is blocking access. Try to set `SeLinux` to `Permissive` and check (if using SeLinux). Never use `777` unless it is necessary why do you need execute permission for a file to read it? – bansi Mar 01 '14 at 12:44
  • Are you absolutely sure that code is producing that result? There's no "can't open the file" string in the code you pasted. Somehow I think we are missing something. – juanrpozo Mar 01 '14 at 12:46
  • the die statement just below the line 2 @juanrpozo – Deepu Mar 01 '14 at 12:48
  • Says "cann't" not "can't". Which line is line 1? Where is $counter_file defined? Apparently it is defined as "/home/username/www/practise/counter.txt" but the Warning message says only "counter.txt". – juanrpozo Mar 01 '14 at 13:03

1 Answers1

1

Maybe you have permission on the directory but not on your file. Use chmod(777) on your txt file. Keep in mind, it's not the best to set 777.

Debflav
  • 1,131
  • 7
  • 17