Page 1 of 1

File Upload help

Posted: Sat Sep 24, 2005 4:24 am
by grovesy
Hi all.

Im very new to the world of PHP and found a nice piece of code to allow file uploads, well it would be nice if it worked!

I have a HTML page that allows the user to browse for a file, when the "Upload" button is pressed the information is passed to my test.php page. Unfortunately i keep getting an error warning error:

Code: Select all

Warning:  move_uploaded_file(/home/fhlinux200/j/jameson-groves.co.uk/user/Yoda.jpg): failed to open stream: Permission denied in /home/fhlinux200/j/jameson-groves.co.uk/user/htdocs/test/test.php on line 9

Warning:  move_uploaded_file(): Unable to move '/tmp/phptmOb5R' to '/home/fhlinux200/j/jameson-groves.co.uk/user/Yoda.jpg' in /home/fhlinux200/j/jameson-groves.co.uk/user/htdocs/test/test.php on line 9

Possible file upload attack!
Can anyone tell me what this means!

This is my php code

Code: Select all

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/home/fhlinux200/j/jameson-groves.co.uk/user/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>
Thanks

Posted: Sat Sep 24, 2005 4:59 am
by s.dot
The directory you're attempting to upload to doesn't have the proper permissions.

Code: Select all

chmod("/the/directory/your/uploads/go/to", 0777);
or you can do it manually through most FTP clients ;d

Posted: Sat Sep 24, 2005 5:14 am
by grovesy
Showing my non skills here, but do I but that line somewhere in the PHP file?

Thanks again

Posted: Sat Sep 24, 2005 5:19 am
by s.dot
put it right after your $upload_dir = 'blah'; line
use

Code: Select all

chmod($upload_dir, 0777);

Posted: Sat Sep 24, 2005 6:30 am
by grovesy
Cheers scrotaye, I think im getting closer, but think im defining the path where the file is going to be stored wrong!

Code: Select all

//$uploaddir = '/home/fhlinux200/j/jameson-groves.co.uk/user/';
//$uploaddir = '/home/fhlinux200/j/jameson-groves.co.uk/htdocs/test/';
$uploaddir = 'pics/';
//$uploaddir = '/pics/';
chmod($upload_dir, 0777);	// Create permissions to write to dir
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
as you can see i've tried many combinations but still getting error messages along the lines of:

Code: Select all

Warning: chmod(): No such file or directory in /home/fhlinux200/j/jameson-groves.co.uk/user/htdocs/test/test.php on line 10
File is valid, and was successfully uploaded.
Any other tips?

Cheers

Posted: Sat Sep 24, 2005 6:36 am
by shiznatix
pics is not in the test folder! or maybe (probably not) you need to do ./pics/ as your uploaddir, but just make sure that the folder exists first

Posted: Sat Sep 24, 2005 6:45 am
by grovesy
Thanks for your help all.

Its now working. Interestingly it may have been working before as it was only a warning message.

Can someone tell me, does the chmod($upload_dir, 0777); bit only have to be run once just to give the folder permissions?

Cheers again

Posted: Sat Sep 24, 2005 6:46 am
by s.dot
grovesy wrote:Thanks for your help all.

Its now working. Interestingly it may have been working before as it was only a warning message.

Can someone tell me, does the chmod($upload_dir, 0777); bit only have to be run once just to give the folder permissions?

Cheers again
only needs to be run once