views:

276

answers:

2

I just setup a LAMP development server and am still trouble-shooting some things. The server is installed on one computer and I use a Windows laptop to write my code and test the site via the web browser.

My file uploading script works in that JPEG image files are successfully uploaded to the server, but when I try to view the images in the web browser, permission is denied.

I check the permissions on the file via the server and they are 600. I can fix the issue by chmod 777 theimage.jpg, but this doesn't seem like a good solution at all.

Does the solution have something to do with Apache configuration? Or is there something else I should be doing.

Thank-you, Mike

Update

To clarify, I am able to upload a JPEG file to /var/www/test/images, but am unable to view the image in the web browser after it has been uploaded. My script works on the production server (I am using Dreamhost). This leads me to believe that the issue is with the development server that I have just setup. Any feedback is greatly appreciated, even if it is just resources that I should read for better understanding the server setup.

A: 

I'd say you probably are running PHP under a different uid than Apache. You can:

  • Configure apache/PHP so that they run under the same uid
  • Upon file upload, use PHP tochange the permissions with the chmod function or change the umask associated with the PHP process so that the file gets the correct permissions in the first place
  • Access the images through PHP (readfile) -- not recommended for performance issues
Artefacto
@Artefacto Thank-you for your response. I am reading about your suggestion that Apache and PHP be run under the same uid.
letseatfood
+1  A: 

You need to change the permissions on the folder containing the file, not just the file itself. Use sudo chmod and sudo chown on the directory that contains the file you want to access, then check to make sure the permissions where changed with the ls -rl command. The permissions used with chmod should be r and w and the directory should read -rw-r--r-- when the ls -rl command is used if the permissions have been changed correctly. Also, if you are still unclear about the specifics of how chmod and chown work check out this link and this link.

EDIT:

Type this:

sudo chmod -R 666 /var/www/test/images

Or this:

sudo chmod a=rw /var/www/test/images

... to do what you want to do. For more explanation see my latest comment entry below.

typoknig
@typoknig I used `sudo chmod -R -rw-r--r-- /var/www/test/images` and `sudo chown -R myusername /var/www/test/images` but the permissions on files uploaded after doing so are `-rw-------`. I am going to try @Artefacto's suggestion and then return to yours. Thank-you.
letseatfood
I think you might have misunderstood me. When you VIEW the permissions with `ls -rl` it should read `-rw-r--r--`, when you use the chmod command it should look like this `sudo chmod -R 666 /var/www/test/images` which will result in permissions `-rw-rw-r--`. You can use the other options too. If you wanted EVERYONE to have permission to read and write the file you could use the 666 command I just mentioned or you could use this command: `sudo chmod a=rw /var/www/test/images`. There are a lot of options :) Here is another link that might help:http://www.zzee.com/solutions/chmod-syntax.shtml
typoknig
@typoknig Thanks for clarifying. I have tried your suggestions, but am still unable to view image files that have been uploaded to the directory.
letseatfood
Perhaps this will help. I currently have used `sudo chmod -R 777 /var/www/test/images`. Then I uploaded an image via my PHP script. The image was uploaded successfully. I cannot view the image by accessing it via the web browser. Running `ls -rl` on `/var/www/test/images/theimage.jpg` shows permissions as `-rw-------`. Does that help?
letseatfood
The `-rw-------` is telling you that the owner of your linux machine is set to read and write permissions. See my edited answer for the two commands you can use. One of them is exactly like the one you already tried except it uses `666` instead of `777`. Also, just in case you didn't know, your privileges are listed in the order of `owner` then `group` then `everyone else` so `-rw-rw-r--` will mean your owner can read and write, the group can read and write, and everyone else can just read.
typoknig
@typoknig Sorry for the late reply. I got caught up with a bunch of other stuff. I am not actually sure this solved my issue. I ended up needing to modify settings in `/etc/vsftpd.conf` on the server, as this ended up being an issue with the FTP user's permissions (since I was using PHP FTP functions to upload the files). Thanks for your help!
letseatfood
I'm glad you got it working
typoknig