[SOLVED] need help with move_uploaded_file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

[SOLVED] need help with move_uploaded_file

Post 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?
Last edited by charp on Sat Jul 02, 2005 12:56 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you need to make sure the web user has write permissions to the folder.
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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...
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post 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?
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

you could upload the file to a tmp directory, copy the file to the correct location and chmod the file etc.
smo
Forum Newbie
Posts: 20
Joined: Tue May 31, 2005 11:02 pm

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I think you probably want to use 644...
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post 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???
smo
Forum Newbie
Posts: 20
Joined: Tue May 31, 2005 11:02 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
charp
Forum Commoner
Posts: 85
Joined: Sun Oct 26, 2003 3:00 pm
Location: Rancho Cucamonga, Calif. USA

Post 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!
Post Reply