File Upload help

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
grovesy
Forum Newbie
Posts: 4
Joined: Sat Sep 24, 2005 4:19 am

File Upload help

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
grovesy
Forum Newbie
Posts: 4
Joined: Sat Sep 24, 2005 4:19 am

Post by grovesy »

Showing my non skills here, but do I but that line somewhere in the PHP file?

Thanks again
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

put it right after your $upload_dir = 'blah'; line
use

Code: Select all

chmod($upload_dir, 0777);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
grovesy
Forum Newbie
Posts: 4
Joined: Sat Sep 24, 2005 4:19 am

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
grovesy
Forum Newbie
Posts: 4
Joined: Sat Sep 24, 2005 4:19 am

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply