问题
My PHP script writes to a file so that it can create a jpg image.
fwrite($handle, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($handle);
print $newfile.'.jpg';
I have put this script on a new server however the image never gets saved. The permission of the folder it saves to is 755 but it does not own it. Last time, I think I fixed the issue by changing the directory owner to apache as this is what PHP runs as. I can not do the same again because I am not root.
Firstly., Is there another fix? Secondly, If I could change the owner of the directory like last time will this fix the issue?
Thanks all for any help
回答1:
- change permissions of the folder to 777 (
rwxrwxrwx) - from PHP create subdirectory with mkdir
- change your directory permissions back to 755 (
rwxr-xr-x) or even better 711 (rwx--x--x) - from now on save your images in the subdirectory, which is now owned by www-data (or whichever user Apache uses).
BTW. you can reduce following code:
fopen($newfile.'.jpg','wb');
fwrite($handle, $GLOBALS['HTTP_RAW_POST_DATA']);
fclose($handle);
print $newfile.'.jpg';
to:
file_put_contents($newfile.'.jpg',
file_get_contents('php://input',
FILE_BINARY),
FILE_BINARY)
回答2:
If you're not the owner, then yes, the directory being 755 is a problem.
To break it down, 755 is:
- user (owner) has read (4), write (2), and execute (1)
- group has read (4) and execute (1)
- others have read (4) and execute (1)
Notice that only the owner has write privileges with 755.
P.S. fopen should return an error if the open failed.
回答3:
use this:
>chown dir_name www-data
回答4:
I had a similar problem a couple of weeks ago. It ended up being the PHP safe mode set in the server.
Check that out!
回答5:
Put error_reporting(E_ALL) at the top of your script and check if it prints out any alert while trying to save an image.
来源:https://stackoverflow.com/questions/678388/write-to-directory-using-php-is-this-a-permissions-problem