View Full Version : PHP, IIS 7, File Upload Problems

03-03-2008, 12:31 AM

I recently started hosting my own website on my computer, instead of a third party hosting site. I set up the databases, included PHP and everything was running fine under IIS 7 on Vista. However, I have a file upload script that no longer works on my site. the code stayed the same from the other hosting service and on my computer. So I am basically asking whether there a setting or something that needs to be changed in order to allow file upload. In my fileupload.php code, it returns "Invalid File" when the criteria for the image is not met, and every time I try to upload a file in the criteria, I receive that error.

Here is my code:

<form accept-charset="UNKNOWN" action="fileupload.php" method="post" enctype="multipart/form-data">
<label for="file">Picture Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />


if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 850000))
if ($_FILES["file"]["error"] > 0)
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("uploads/" . $_FILES["file"]["name"]))
echo $_FILES["file"]["name"] . " already exists. ";
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
echo "Invalid file";

03-03-2008, 01:07 AM
Are you getting any errors? You may have to set the permissions on the folder that you are trying to upload to.

03-03-2008, 01:25 AM
I'm not getting any errors surprisingly. It's just skipping down to the "else statement" as if the image does not fit the criteria.

Also, I have shared the uploads folder with "IIS_IUSRS" making it a co-owner. I am a little unfamiliar with Vista but to my knowledge making IIS a co owner give it all privileges to the directory.

The php.ini file I have allows file uploads with a max limit of 2MB, which I am well under, It just seems weird that the same code works with my other host, but not when I myself host my site.

Thanks for any/anymore input provided.

03-03-2008, 01:44 AM
I know you aren't using wordpress but the tips here might help.

03-03-2008, 01:48 AM
what do you see when you
<?php print_r($_FILES);?> ?

03-03-2008, 01:56 AM
I keep seeing that same crappy upload code. Where are people copying that from?

It does not check the ['error'] element until after it checks the ['type'] and ['size']. People, if there is an error, the type and size will be empty and testing them will fail. You must check for errors before you ever touch any of the data.

Also, by combining all the type and size testing into one conditional statement and one error message, you will never know which test failed.

The error message, which is just echo "Invalid file"; now, needs to output a meaningful message as to what exactly was wrong and what the value was, for example echo $_FILES["file"]["type"]; as part of the error so that you know what was actually received.

It is likely that file uploading is disabled on your server or the temp location for uploading files to does not exist or does not have the proper permissions. You need to check all the runtime values related to uploads using a phpinfo() statement.

I won't even suggest turning on full php error reporting and then checking your web server log file because you probably did those troubleshooting steps before posting your problem in a forum.

03-03-2008, 01:57 AM
what do you see when you
PHP Code:
<?php print_r($_FILES);?>

Parse error: syntax error, unexpected T_STRING in C:\inetpub\wwwroot\fileupload.php on line 7

I get the above error...

03-03-2008, 06:36 AM
The posted code does not produce that error. In fact, the posted code does not produce any fatal php parse errors.

You would need to post the actual code that caused that error to get help with what is causing it.