Page 1 of 1

[SOLVED] need help with move_uploaded_file

Posted: Fri Jul 01, 2005 8:44 pm
by charp
I've been running in circles throughout the day trying to figure out how to upload a file by way of an HTML form and save it in a particular directory. It seems to be a permissions issue that I keep running into:

Warning: move_uploaded_file(./pics/quote_start.gif): failed to open stream: Permission denied in /home/uplandw/public_html/resamp.php on line 16

Code: Select all

move_uploaded_file($tmp_name, $filename);
I'm working on a shared hosting server. Could this be the source of my permission issues or have I not stumbled across the correct paths for this function?

Posted: Fri Jul 01, 2005 8:54 pm
by Burrito
you need to make sure the web user has write permissions to the folder.

Posted: Fri Jul 01, 2005 9:10 pm
by charp
ummmm.... 'kay... I can do this, but let's just pretend that I'm not sure how to begin...

Ah heck, there's no pretending. I need more help.

I have the destination directory CHMOD'd to 755 and the php file that's doing the writing set to 644. Is this the sort of permissions you're suggesting?

Also, if anyone can point me to a really good online tutorial for uploading files and chmoding via php, I'd be very appreciative.

Posted: Fri Jul 01, 2005 10:00 pm
by Burrito
a simple google search found this:
http://www.netspade.com/articles/php/uploading.xml

or when in doubt always check the manual:
http://www.php.net/manual/en/features.file-upload.php

look at chmod() for help changing the permissions...

Posted: Fri Jul 01, 2005 10:08 pm
by charp
Burrito,

Thanks for the hint in your first post. I played around with the chmod value for the destination directory and managed to get everything to work. However, I had to set directory to 777. Is this an inherently unsafe setting for a directory? Perhaps I should be chmoding it to 777 to save the file and then chmoding it back to a safer setting? Any thoughts?

Posted: Fri Jul 01, 2005 10:31 pm
by facets
you could upload the file to a tmp directory, copy the file to the correct location and chmod the file etc.

Posted: Fri Jul 01, 2005 11:37 pm
by smo
charp wrote:Burrito,

Thanks for the hint in your first post. I played around with the chmod value for the destination directory and managed to get everything to work. However, I had to set directory to 777. Is this an inherently unsafe setting for a directory? Perhaps I should be chmoding it to 777 to save the file and then chmoding it back to a safer setting? Any thoughts?
Yes that is the way, By script also you can do that.
chmod("$dir_path",0666);

I have written one simple article at http://www.plus2net.com/php_tutorial/ph ... upload.php

HTH

Posted: Fri Jul 01, 2005 11:48 pm
by Burrito
I think you probably want to use 644...

Posted: Sat Jul 02, 2005 1:02 am
by charp
Well, still going in a bit of a circle.

I tried to follow smo's advice and chmod the directory before using move_uploaded_file and then chmod back afterwards.

However, I get this error message:

Warning: chmod(): Operation not permitted in /home/uplandw/public_html/resamp.php on line 12

Not permitted? I can't see my way around this problem.

I thought about using facet's suggestion, but uploading to a tmp directory and copying to another directory still requires the 777 setting, doesn't it?

Perhaps this could all be done via ftp, but surely there must be a straight PHP solution???

Posted: Sat Jul 02, 2005 1:13 am
by smo
Try giving 777 to the dir through FTP , if that works then upload with that permission. But by FTP if you are not able to give 777 then you don't have that permisssion to do, so with script also you won't get.

Posted: Sat Jul 02, 2005 7:25 am
by Chris Corbyn
charp wrote:Warning: chmod(): Operation not permitted in /home/uplandw/public_html/resamp.php on line 12

Not permitted? I can't see my way around this problem.
I've seen this too many times on this forum.

Note: If PHP doesn't have write permissions to a directory it certainly doesn't have chmod() permissions.

You need to use FTP or SSH to do the CHMOD if PHP is getting permissions errors ;)

Posted: Sat Jul 02, 2005 12:56 pm
by charp
d11wtq has it right!

I check with my hosting company -- here's an excerpt from their reply:
PHP scripts run under the user ID of the web server, which is 'nobody'. This means that PHP scripts can only CHMOD files and directories that are owned by the user 'nobody'. In your case, $upload_dir is owned by you (your user ID), so your PHP script is not allowed to change that directory's permissions with CHMOD.
I tried to use mkdir in an attempt to create a directory owned by 'nobody', but that too failed because the script does not have permission to write to the parent directory. Elsewhere, I read that shared hosting environments typically disable chown so that option is out as well.

In the end, the only options are to chmod the directory to 0777 and leave it there or use FTP to get the job done.

Thanks to all who replied!