1

I am using a basic file read write function in a middle ware of slim framework 3.

$file = __DIR__ . '\\result.json';
$fh = fopen($file, 'w') or die('Cannot open files:  '.$file); 

this file exist and path is correct. I also have the permission to read and write the file.

The result is showing cannot open files.

Please suggest how a simple read and write function will work in slim framework 3.

Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49
Vijay
  • 41
  • 5
  • Well there is no difference in opening files in slim, are you sure that this path exists why do you use a double backslash (\\\) inside your file path? – jmattheis Dec 13 '16 at 08:37
  • I have tried with (\), (/) both, and when I am echo $file it is showing right path. I am sure about the right path of file. – Vijay Dec 13 '16 at 08:45
  • use [`DIRECTORY_SEPARATOR`](http://php.net/manual/en/dir.constants.php) for this – jmattheis Dec 13 '16 at 08:47
  • No success... I have tried this also `$my_file = __DIR__.DIRECTORY_SEPARATOR.'result.json';` – Vijay Dec 13 '16 at 09:25
  • remove or die() and [enable error handling](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) and or look at you php log for exceptions – jmattheis Dec 13 '16 at 09:30
  • Looks like a permissions problem. Change the permissions of your folder to include write permissions – geggleto Dec 13 '16 at 15:00
  • Not Slim issue. Most likely your webserver does not have write access to the file. See your error logs. – Mika Tuupola Dec 14 '16 at 04:09
  • file exist is not necessary because handler with 'w' `$fh = fopen($file, 'w')` check as die($file); and you see path to file. possible that the path wrong – 1f7 Dec 17 '16 at 00:58

0 Answers0