PDA

View Full Version : File Upload has to go to same directory as script?


mothra
11-01-2005, 04:12 AM
When I attempt to upload files I get the warning:
move_uploaded_file(/home/csadmin/public_html/up/guide.jpg): failed to open stream: Permission denied in /home/csadmin/public_html/bod/upload.php on line 21

I don't pretend to know what I'm doing, but it looks like my file upload needs to be in or under the same directory as the upload script itself? I tested this theory by creating a sub directory there and running the script. I end up with a permissions error at that point, attempting to upload to any other location produces the warning above.

Something else I noticed is I don't see the "csadmin" directory anywhere, I guess this is probably my host directory.

Any help would be appreciated, I thought that I can upload to any directory under the web root (public_html).



Here is the code by the way, copy and pasted from the manual.

if (isset($_POST['submit'])){
$uploaddir = '/home/csadmin/public_html/up/';
$uploadfile = $uploaddir. $_FILES['userfile']['name'];

print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
print "</pre>";
}

Fou-Lu
11-01-2005, 04:24 AM
you can. You can actually do it above the web root as well.
Can you get it to upload to the current directory? Cause if you can its a simple fix of chmod to your new folders permissions 0700 or 0777. This can be done temporarly by php for use with the script, or perminantly using ftp or anything of the sorts. If you cannot upload to the directory after checking the chmod, then you may be out of luck as your server may not allow uploads. You can check this in your phpinfo(), check for safe_mode and open_basedir.
If your safemode is enabled, you will need to seek another method of doing this. There are work arounds.


BTW, I'd try altering two things with your code as well, first try to make this:
$uploaddir = '/home/csadmin/public_html/up/';
a relative path:
$uploaddir = './up/';
and check for the existance of $_FILE['userfile']['tmp_name'] prior to the upload:

if (!empty($_FILE['userfile']['tmp_name']))
{
if (move_uploaded_file( ....
}

This will prevent an E_NOTICE from appearing with error_reporting.

mothra
11-01-2005, 04:36 AM
Thanks for your suggestions.

safe_mode = off
open_basedir = no value

I've tried chmod on the directory in question but I just get the following:
Warning: chmod(): Operation not permitted in /home/csadmin/public_html/bod/upload.php on line 21


I have been unable to upload to any directory.