File Upload problem

A

Anonymous

Guest
Hi,

I am using the following script to try an upload a file from an HTML form to my local webserver (Apache / Php 4 on WinXP). I am doing this to test and try and learn how to do this. My PHP script is as follows:

Code:
<?php

$userfile = $_FILES['userfile']['name'];
if (is_uploaded_file($userfile)) {
  move_uploaded_file($userfile, '/upload');
}

echo $_FILES['userfile']['error'];
print_r($_FILES);
?>

the form is working as i get the following info when i hit submit on the form:

Code:
0
Array ( [userfile] => Array ( [name] => 4.jpg [type] => image/jpeg [tmp_name] => C:\WINDOWS\TEMP\php3.tmp 
[error] => 0 
[size] => 74525 ) )

the error code returned is 0 which should mean that the file was uploaded.
/upload is below htdocs in the Apache folders.

I would be grateful if someone could point out what I am doing wrong as it is driving me up the wall. I have tried putting the full windows path as the upload directory but that didn't work. The upload tmp dir in php.ini is also pointing at the same directory.

Many thanks for any replies.

luke.
 
The first parameter to move_uploaded_file(), "filename", refers to the name/path of the temporary file ($_FILES['userfile']['tmp_name']), NOT the $_FILES['userfile']['name']. In other words, you should have $userfile = $_FILES['userfiles']['tmp_name'] on that first line.
 
i have changed the code to

Code:
<?php

$userfile = $_FILES['userfile']['tmp_name'];
if (is_uploaded_file($userfile)) {
  move_uploaded_file($userfile, '/upload');
}

echo $_FILES['userfile']['error'];
print_r($_FILES);
?>

but the file is still not uploading.
 
What error are you getting? After upload, does the temp file exist? Are its contents correct? Is your path correct (e.g. does the '/uploads' directory exist? try changing it to a relative path).
 
i think the temp file gets deleted if the upload isn't successful - its not there anyway. however, i've managed to get the following to work:

Code:
<?php

$newfile = 'C:\Program Files\Apache Group\Apache2\htdocs\upload\5.jpg';

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $newfile)) {
	
   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>";

?>

if i upload a jpg and specify the exact filename for the new file, this works. however, if i try to create a $uploaddir variable and then do:

Code:
//$uploaddir = 'C:\Program Files\Apache Group\Apache2\htdocs\upload\';
//$newfile = $uploaddir . $_FILES['userfile']['name'];

it doesn't work - no error message. in the php error log, there is...

[client 127.0.0.1] PHP Parse error: parse error, unexpected T_STRING

thanks for your help so far. if you could shed any light, i'd be very grateful .

rgds,

luke.
 
hi,

i need an extra \ as an escape character. d'oh. final code:

Code:
<?php


$uploaddir = 'C:\Program Files\Apache Group\Apache2\htdocs\upload\\';//extra slash needed for escape char.
$newfile = $uploaddir . $_FILES['userfile']['name'];

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $newfile)) {
	
   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>";

?>

thanks again for your help.

rgds,

luke.
 
You should turn error_reporting to E_ALL on your development system; it'll save you lots of trouble in the long run.
 
is that in php.ini somewhere?

i seem to have found a good forum - thanks for the replies.

if you like t-shirts, take a look at http://www.lukem.co.uk and i'll give you a discount.

<L>
 
lukemack said:
i have changed the code to

Code:
<?php

$userfile = $_FILES['userfile']['tmp_name'];
if (is_uploaded_file($userfile)) {
  move_uploaded_file($userfile, '/upload/'.$_FILES['userfile']['name']);
}

echo $_FILES['userfile']['error'];
print_r($_FILES);
?>

but the file is still not uploading.
 
Back
Top