...

View Full Version : upload png showing as invalid



harkly
10-29-2012, 12:34 AM
I am using this script to upload files for testing, however it will not upload any png files. Always get an "Invalid Files" error

http://www.w3schools.com/php/php_file_upload.asp



$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}


<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submitPhoto" value="Submit" />
</form>

tangoforce
10-29-2012, 11:39 AM
At the top of your code you should:
var_dump($_FILES);

Then look at the html source, copy it, paste it here in a reply.

As for w3schools.. I don't know why they continue with their tutorials. They're useless yet because they offer a few fairly meaningless qualifications that look impressive on a CV, they are thought of as the 'bees knees' in the programming world.

abduraooft
10-29-2012, 01:13 PM
Possibly because of
$_FILES["file"]["size"] < 20000 in your code. You might be trying to upload an image having size >20kb

harkly
10-29-2012, 02:06 PM
this is what I get with the var_dump($_FILES);

array(1) { ["file"]=> array(5) { ["name"]=> string(8) "TEST.PNG" ["type"]=> string(11) "image/x-png" ["tmp_name"]=> string(38) "/home/content/01/6514501/tmp/phpLba12E" ["error"]=> int(0) ["size"]=> int(9091) } } Invalid file

I know its not the size. The other image files load as they should at much larger sizes. But just to be sure I've removed the file size limit and it still won't work

The thing is that particular code was working for all the image files and now its not just for the png images. Is there any other setting that could prevent them from loading?

Looks like this is giving me the problem "image/x-png" if I add that to my code then it uploads the image


|| ($_FILES["file"]["type"] == "image/x-png")

What is that about?

Also do you know a good tutorial on uploading images, that is not W3school? and is simple?

tangoforce
10-29-2012, 03:07 PM
Looks like this is giving me the problem "image/x-png" if I add that to my code then it uploads the image


|| ($_FILES["file"]["type"] == "image/x-png")

What is that about?

So stick with that then. It looks like the mime tye label is different for png than it is jpg / gif / bmp etc. No idea why but it looks like thats the reason here.



Also do you know a good tutorial on uploading images, that is not W3school? and is simple?

Google is full of them :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum