PHP uploading files help

cHeeZeFacTory

Golden Member
Apr 23, 2001
1,658
0
0
I'm trying to write a simple script to upload files onto a network server...but it's not uploading the files at all...

EDIT: using windows server 2003

<?php

$uploaddir = '/inetpub/wwwroot/upload/';

if(is_dir($uploaddir))
{echo "valid directory!\n";}
else
{echo "not a valid directory.\n";}

if(is_writable($uploaddir))
{echo "writable<br>";}
else
{echo "not writable<br>";}

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<br>".$uploadfile;
echo "<br>".getcwd();

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";
}

print_r($_FILES);
unlink($_FILES['userfile']['tmp_name']);


echo "</pre>";
?>

I GET THIS OUTPUT:

Possible file upload attack!
Array
(
[userfile] => Array
(
[name] => menu_test.css
[type] => text/css
[tmp_name] => C:\WINDOWS\TEMP\php301.tmp
[error] => 0
[size] => 2084
)

)
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
What's the output from the echo "<br>".$uploadfile; command? You may be missing a '/' between the $uploaddir &amp; basename($_FILES['userfile']['name']);
Also, I've never had consistant results using move_uploaded_file on a windows apache install, use the rename command instead.
You can also lose the unlink command at the end, you're moving the file, and even if the move fails, temp files are deleted after the process ends.
 

cHeeZeFacTory

Golden Member
Apr 23, 2001
1,658
0
0
i added echo $uploadfile to see if I had the directory correct. I don't think the '/' is needed, when i print out $uploadfile i get:

/inetpub/wwwroot/uploadtest.txt
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
shouldn't that be:
/inetpub/wwwroot/upload/test.txt

change the line that says:
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
to:
$uploadfile = $uploaddir . '/' . basename($_FILES['userfile']['name']);
 

cHeeZeFacTory

Golden Member
Apr 23, 2001
1,658
0
0
some more info:

when i use the function:

is_readable($_File['userfile']['tmp_name']);

function returns false

can this be the cause of the problem?

temp directory is c:\windows\temp\
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
Originally posted by: cHeeZeFacTory
some more info:

when i use the function:

is_readable($_File['userfile']['tmp_name']);

function returns false

can this be the cause of the problem?

temp directory is c:\windows\temp\

could be.
try setting it to something like c:\PHP\uploadtemp, and make sure that directory exists and has full read + write permissions.
 

cHeeZeFacTory

Golden Member
Apr 23, 2001
1,658
0
0
Originally posted by: Beau
If you haven't created the directory, how are you going to move the files there?

try this out, works here:



shouldn't
if(is_writable($uploaddir))
die($uploaddir . ' is not writeable. Aborting upload.');

be !is_writable ?
 

cHeeZeFacTory

Golden Member
Apr 23, 2001
1,658
0
0
Originally posted by: Beau
If you haven't created the directory, how are you going to move the files there?

try this out, works here:

just tried it and my output was "File uploaded, but could not be moved to /inetpub/wwwroot/upload_files/test.txt"

probably a permissions problem w/ the temp folder...I'll have to contact the server admin.

Thanks Beau!!
 

cHeeZeFacTory

Golden Member
Apr 23, 2001
1,658
0
0
IS has changed the upload_tmp_dir to "C:\Inetpub\wwwroot\upload"

but when I echo $_FILES['userfile']['tmp_name'];

it still says C:\windows\temp\ , ???