View Full Version : File Upload will not work! No error, yet no upload?

12-19-2008, 09:54 PM
Hello. I am pretty new at php, and for this project I am actually just moving a website from it's old Apache server to our new Windows server. I've gotten PHP setup on the server, the mySQL database is setup and working correctly. This seems to be the last issue.

Here is my code:

// if $_FILES['thefile'] isn't empty, try to copy the file
if ($_FILES['thefile'] != "") {

$myfile = $_FILES['thefile']['name'];

if (is_uploaded_file($myfile)) {
print "Uploaded file found";
// move the file here
move_uploaded_file($_FILES['thefile']['tmp_name'], "../Access/files/".$_FILES['thefile']['name'])
or die("<b>Couldn't move the file!</b>");
} else {
print "Error Code: ".$_FILES['thefile']['error']." <br>File: ".$_FILES['thefile']['name']." <br>Temp: ".$_FILES['thefile']['tmp_name'];
} else {

// if $_FILES['thefile'] was empty, die and let us know why
die("No input file specified");

I always get the following output when trying to upload the file:

"Error Code: 0
File: Bylaws of.pdf
Temp: E:\temp\phpUploads\php83F3.tmp"

There is never any files in my temp directory, nor in the destination directory.

What am I doing wrong? This code was used on the old server with no problems. I changed the temp directory and destination directory, as well as set "Everyone" with full Permissions in both folders on the server (just to be sure there wasn't a permissions issue).

What is going on here? Can someone shed some light?


12-19-2008, 10:16 PM
The temporary files are deleted by php when the script ends, so unless you are fast or you deliberately introduce a delay in the script's execution, you will never see the files in the temp folder.

is_uploaded_file() only works on the ['temp'] element, so the posted code will always execute the code in the "else" branch of the logic.

12-19-2008, 10:30 PM
Well that makes sense that it would test is_uploaded_file against the temp file.....I guess I wasn't thinking there.

So I changed the is_uploaded_file to test for the tmp_name of the file, and alas it did show "Uploaded file found." But then it had the message: "Couldn't move the file!", which is the error when move_uploaded_file() fails.

Any ideas why this might be happening? I feel like I am so close now. Thanks again.

12-19-2008, 10:49 PM
Add the following two lines of code immediately after your first opening <?php tag -

ini_set ("display_errors", "1");

12-19-2008, 11:07 PM
Ok I think I've got the problem figured out, but I still need a little more direction.

I just had the realization that the Destination path was probably relative to the temporary directory, not the directory that my php file was in. (My .php file was in "siteroot/admin/do_mod_bylaws.php" and I wanted the file to upload to "siteroot/access/files/"). So I changed my temp directory (in php.ini) to "siteroot/temp/", so that my relative file path ("../access/files/") would work. And it did!

So now my question: I currently have the permissions for "Everyone" set to "Full Control" for both the temp directory and the Access/files directory. I know this is not the best/safest way to achieve write permissions. Is there a specific user that php uses (similar to "Network Service/NT Authority" that ASP.net uses), that way I don't have to give full control to these directories to Everyone?

In other words, what do I need to do (and for which users) with my directory permissions to allow file uploads?