Thomas Thomas - 3 months ago 12
PHP Question

Cannot copy or move uploaded files in Windows Server

(The question was initially asked in Server Fault. I move it to here, because I don't have enough credit to start a bounty over there.)

I am trying to make my

Windows Server 2012 R2
to host a webpage where users could upload a file.

<!-- indexfile.html -->
<form action="uploadfile.php" method="post" enctype="multipart/form-data">
Browse for File to Upload: <br>
<input type="file" name="file" id="file" size="80"> <br>
<input type="submit" id="u_button" name="u_button" value="Upload the file">
</form>


// uploadfile.php
<?php
$file_result = "";
if ($_FILES["file"]["error"] > 0)
{
$file_result .= "No File Uploaded or Invalid File ";
$file_result .= "Error Code: " . $_FILES["file"]["error"] . "<br>";
} else {
$file_result .=
"Upload: " . $_FILES["file"]["name"] . "<br>" .
"Type: " . $_FILES["file"]["type"] . "<br>" .
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>" .
"Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

if (chmod("C:/inetpub/testaddbbacom/test", 0777))
$file_result .= "chmod sucessful!<br>";
else
$file_result .= "chomod NOT sucessful!<br>";

copy($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["tmp_name"]);

$file_result .= "File Upload Successful!";

echo $file_result;
}
?>


The problem is, files can be uploaded into
C:\Windows\Temp\
, whereas
copy
does not work. As consequence,
C:\inetpub\testaddbbacom\test
is always empty. I did try to change the permission of this folder by
chmod
, but it does not really work. Here is a comparaison of the permissions of two folders:

enter image description here

I also tried
move_uploaded_file
, but it did not work either, probably due to the same permission reason...

Does anyone know how to solve this problem?

Answer

Replace,

copy($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["tmp_name"]);

With the following,

move_uploaded_file($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["name"]);

the difference is use file name i.e $_FILES["file"]["name"]) and not $_FILES["file"]["tmp_name"]) in destination path.