Write to Directory using PHP: Is this a permissions problem?

若如初见. 提交于 2019-12-08 03:47:23

问题


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:


  1. change permissions of the folder to 777 (rwxrwxrwx)
  2. from PHP create subdirectory with mkdir
  3. change your directory permissions back to 755 (rwxr-xr-x) or even better 711 (rwx--x--x)
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!