Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fopen: failed to open stream: Permission denied in PHP on Mac [duplicate]

Tags:

php

macos

I wrote this piece of code:

if (file_exists("testfile.rtf")) echo "file exists.";
else echo "file doesn't exist.";

$fh = fopen("testfile.rtf", 'w') or die("impossible to open the file");

$text = <<< _END
Hi buddies, 
I write for the first time
in a PHP file!
_END;

fwrite($fh, $text) or die("Impossible to write in the file");
fclose($fh);
echo ("writing in the file 'testfile.rtf' succeed.");

But when I run it it says:

fopen(testfile.rtf): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/test/test1.php on line 64 impossible to open the file

I use XAMPP on my MAC to work on local server.

I found several topics where some people had the same problem, and solved it using something called "chmod 777" but I don't know what that means, where to write this, etc.

like image 316
Uj Corb Avatar asked Feb 07 '23 03:02

Uj Corb


1 Answers

The error you are seeing simply means that your php script does not have the permission to open testfile.rtf for writing.

do not "chmod 777" everything anytime you are getting permission issues, this might introduce security problems later! instead- give the correct users the correct permissions.

In this case:

Since you are running XAMPP, then you need to make sure that your web server gets the write permission to the specific path you are trying to create new files in and\or edit existing files.

In the case of OSX and apache, I believe the webserver's user is named "_www".

Here's a small example (you might need to change it to your needs):

sudo chown -R _www:_www /path/to/folder

The example above will give _www (webserver) ownership of the /path/to/folder and all of the files in it.

Also: before you do any command-line based administration, you might want to read a little bit about unix commands, such as chown,chmod,etc..

https://www.techonthenet.com/linux/commands/chown.php https://www.techonthenet.com/linux/commands/chmod.php

Hope it helps a bit!

like image 75
codelock Avatar answered Mar 01 '23 23:03

codelock