Pending Pending - 4 months ago 13
PHP Question

PHP - fopen(): failed to open stream: Permission denied on a mac?

I can't get it to work and create a simple file in the same directory.

I'm using a Mac and text editor is Sublime Text, and the php version is 5.5.30.

This is the code I'm executing:

$file = fopen( 'data/data.txt' , 'w' );
if( $file ) {
echo 'The file is open';
fclose( $file );
echo 'The file could not be open.';

This is the error generated in the browser:

Warning: fopen(data/data.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/php_class/class_examples/open_and_close.php on line 17

I searched online for an answer to the error and this is the solution I got to fix the problem:

Open the terminal, go to the directory where the script is located and enter the following command

sudo chmod 777 open_and_close.php

Now I did all of this and I'm still getting the same error.
Any suggestions on what I'm doing wrong?

Thank you.


You have to run the command for : data/data.txt and not for open_and_close.php

sudo chmod 777 data.txt