If you haven't set up PHP on your Mac yet and want to do that, you can go to my walkthrough to get it running in no time.
So, let's get this over with!
Take a look at the following error message:
Warning: fopen(/test.txt) [function.fopen]: failed to open stream: Permission denied in /Users/Me/Sites/PHProject/includes/file_writer.php on line 32
That should look familiar to you, because that's the error you got if you tried writing a local file with PHP before allowing PHP to even write stuff to the directory in question.
The actual code would be something like:
$fh = fopen("/test.txt", "a");
fwrite($fh, "Some String Theory");
fclose($fh);
Simple enough, so why isn't it working? PHP runs under the "www" user on your system and it is this user that we need to give the necessary permissions. Fortunately, doing that is very easy and fast.
All we need to do is run a command in Terminal—yes, just one command!—to give "www" these permissions by means of an Access Control List (ACL).
And here it is:
chmod +a "www allow delete,readattr,writeattr,readextattr,writeextattr,readsecurity,list,search,add_file,add_subdirectory,delete_child,file_inherit,directory_inherit" ~/Sites/PHProject/files
Without going into too much detail I will give you a breakdown of each part of the command:
chmod is the name of the Unix command that let's you change the permission-set of files and folders or edit their ACLs.
+a indicates that we want to add an ACL entry.
The next part, enclosed in quotation marks, says exactly what to do with the ACL.
www is the user we want to change permissions for.
allow means that the following types of file/folder functions (about which you can find out more by typing man chmod in Terminal) should be made available to www.
Lastly, after the quoted ACL instructions, you need to specify, for yourself, which folder this command should be applied to.
Also note that the command above assumes you are targeting a folder and not just a single file. If you are going to run this command on a file alone, take out everything between "readsecurity" and the finishing quotation mark. Either way I recommend you look through the manual page of chmod in Terminal (man chmod, then scroll down a bit by pressing ↓) as that contains concise descriptions and examples.
Anyway, if you ran the command already (and didn't get an error because you forgot to change the path
...unless you wanted the command to work on existing files in that directory (which is a plausible possibility that I forgot to account for), because that will require another one of these commands, for each file (or just one more command if you're a Terminal pro unlike me). So remove the items that are not applicable to files, and add "read,write,append" and "execute" for files that you want PHP to be able to execute. Change the path to point to your file(s) and run the command. Any files that you create in this directory in the future, though, should not require this extra step.
Good luck and please do let me know if it didn't work for you!
Resources used:
- irc.freenode.net ( ##php and #macosx ⇒ thanks krunk, thanks a lot grekkos!)
- man chmod
This post has been edited by temhawk: 15 October 2010 - 07:29 PM


Help


MultiQuote









