6

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.

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Uj Corb
  • 1,959
  • 4
  • 29
  • 56
  • Have you tried running this in your home folder? Also: Does the file actually exist? Please edit your question accordingly. – David Makogon Sep 14 '16 at 19:23

1 Answers1

10

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!

codelock
  • 747
  • 5
  • 8
  • I wrote sudo chown -R _www:_www /Applications/XAMPP/xamppfiles/htdocs/test/testfile.rtf But the Terminal says: no such file or directory – Uj Corb Sep 14 '16 at 22:19
  • @JulienCorbin try it on the folder (without file name) instead – codelock Sep 15 '16 at 04:48