...

View Full Version : Can't upload images in jpeg format



levani
04-03-2010, 11:34 AM
I use this script from http://www.w3schools.com/php/php_file_upload.asp to upload images but can't upload images in jpeg format, it writes Invalid file.

Test in FF and IE. Images in gif format are uploaded without any problem.

Can anyone please help?


<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
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"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

abduraooft
04-03-2010, 01:18 PM
Add the following to the top of above code and check the output after a jpg upload.

echo '<pre>';
print_r($_FILES);
echo '</pre>';

Phil Jackson
04-03-2010, 01:23 PM
what does this reveal?


<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
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"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file: " . $_FILES["file"]["type"];
}
?>

Phil Jackson
04-03-2010, 01:27 PM
if( preg_match( "#^image/((?:gif)|(?:jpg)|(?:jpeg)|(?:png))$#is", $_FILES["file"]["type"], $match ) ){
$extension = $match[1];
}

Phil Jackson
04-03-2010, 01:29 PM
also your file size is stupidly low


$maxFileSize = 1572864; // 1.5MB - max file size of image to be uploaded.

Phil Jackson
04-03-2010, 01:32 PM
try


<?php
if( preg_match( "#^image/((?:gif)|(?:jpg)|(?:jpeg)|(?:png))$#is", $_FILES["file"]["type"], $match ) ){
$maxFileSize = 1572864; // 1.5MB - max file size of image to be uploaded.
if( $_FILES["file"]["size"] < $maxFileSize ) {
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"], "upload/" . $_FILES["file"]["name"] );
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}else{
echo "File size too large ( must be less than 1.5mb )";
}
}else{
echo "Invalid file: " . $_FILES["file"]["type"];
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum