0

I'm having trouble writing a file in my private folder on my school's server. I've heard the problem is my rights. Or is it the code? I believe the folders location is correct. I get a few errors:

1    $testfile = "SchoolServer/Test.txt";
2           
3           if (!file_exists($testfile)) 
4           {
5               $fileoperation = "a";
6               $test = preg_replace( '/\h+/', ' ', $test);
7               $file = fopen ($testfile, $fileoperation);
8               fwrite ($file, $week . "\r\n" . $test . "\r\n" . "\r\n");
9               fclose ($file);
10          }

Warning: file_exists(): open_basedir restriction in effect. File(Test.txt) is not within the allowed path(s): (D:\SitesTemp) in D:\Register-test.php on line 3

Warning: fopen(): open_basedir restriction in effect. File(Test.txt) is not within the allowed path(s): (D:\SitesTemp) in D:\Register-test.php on line 7

Warning: fopen(Test.txt): failed to open stream: Operation not permitted in D:\Register-test.php on line 7

Warning: fwrite() expects parameter 1 to be resource, boolean given in D:\Register-test.php on line 8

Warning: fclose() expects parameter 1 to be resource, boolean given in D:\Register-test.php on line 9

Thanks!

zajonc
  • 1,935
  • 5
  • 20
  • 25
Pandara
  • 39
  • 2
  • 6
  • what Full path are you using in $testfile = "SchoolServer/Test.txt"; is it full path ? Did you tried out with print_r($_SERVER) in your php file? – Dharmendra Aug 29 '16 at 09:21
  • This is written within an if (!file_exists($testfile)) It's purpose is to check if the file exists. If not, it writes it. If it already exists, it doesn't overwrite, but adds content to the already existing file – Pandara Aug 29 '16 at 09:45
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Aug 29 '16 at 17:49

1 Answers1

0

It seems that fopen fails, you should test what it returns :

$file = fopen($testfile, $fileoperation);
if ($file) {
    fwrite($file, $week . "\r\n" . $test . "\r\n" . "\r\n");
    fclose($file);
}

Also, it seems that your file is located in a place where php can't open it, see here

vincenth
  • 1,732
  • 5
  • 19
  • 27
  • That doesn't return anything. No error messages for that command either – Pandara Aug 29 '16 at 09:49
  • It should return either a resource or false if it failed. You need to check you got a resource before using fwrite or fclose. The problem seems to come from where your file is located, try to put it, in the same directory than your php script in order to see if it's working. – vincenth Aug 29 '16 at 09:52
  • I've tried to put it in the same folder. Same problem. Also tried writing the txt-files on my desktop without luck. – Pandara Aug 29 '16 at 10:28
  • Have you read the link about open_basedir : http://php.net/manual/en/ini.core.php#ini.open-basedir ? – vincenth Aug 29 '16 at 10:32
  • 1
    There is a troubleshooting checklist for this error here : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Aug 29 '16 at 17:50