views:

1302

answers:

4

Hi

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

A: 

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!

Seb
I checked this out and it wasn't in safe mode.
Abs
+6  A: 
  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)
vartec
The user php runs as actually doesn't have permission to write files and therefor not to create directories. Thank you for the reduced version, it looks good. :)
Abs
Just changing the directory to 777 worked! A little dangerous yes but I only need it for testing purposes.
Abs
A: 

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.

L. Cosio
+1  A: 

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.

R. Bemrose