Jump to content

noob needs help with file type


oliver9523

Recommended Posts

hi, i've been following the tutorials on this site for PHP and i've got the bit about filtering out files to upload. Tutorial. (sorry in advace for the long post, i don't want to leave anything out just incase)This is the example code...

<?phpif ((($_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 "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];	}  }else  {  echo "Invalid file";  }?>

The following is the code i'm using....

<?phpif ((($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] <= 50000))	{	if ($_FILES["file"]["error"] > 0)	  {		  echo "Error: " . $_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 "Saved in: " . $_FILES["file"]["tmp_name"];	  }	}else	{   if (($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/pjpeg"))	  {	   echo "File too large";	  }	   else	  {	   echo "Wrong type";	  }	}?>

I wanted to give a bit more information as to why the file has been filtered out. That works fine for most files but when i try and upload an image file taken from my Nikon D50 DSLR which is a .jpg the error message goes to "Wrong type" instead of "File too large". So i added in this line below to try and debug it...

	  echo "File too large";	  }	   else	  {	   echo "Wrong type" . $_FILES["file"]["type"];	  }	}?>

The output I get from firefox and IE is just "Wrong type". So for some reason $_FILES["file"]["type"] isn't working for my DSLR pics.I was thinking about using exif_imagetype() but i'm not sure how to use it, can anybody help?cheers.

Link to comment
Share on other sites

Hi.. yes, you are right you can go that way if $_FILES["file"]["type"] is not working for some case.. check via exif_imagetype() function and using the above file-var to get type are you not getting any error?-Vijay

Link to comment
Share on other sites

i've just tried the following...

echo $_FILES["file"]["tmp_name"] . "<br />";echo $_FILES["file"]["name"] . "<br />";echo $_FILES["file"]["type"] . "<br />";echo $_FILES["file"]["size"] . "<br />";

the output when i uploaded a DSLR image wasDSC_2500.jpg0So tmp_name, type and size don't work properly, it works fine for other image files.I was thinking that i might use the exif function something like this exif_imagetype($_FILES["file"]["tmp_name"]) but the tmp_name doesn't work, can anybody give an example of how it's used?

Link to comment
Share on other sites

There's no reason why the temp name should not be filled in. PHP will save the file as a temporary file and put the filename of that file into the array, it doesn't matter what the content of the file is. Print out the $_FILES array to see what's there.print_r($_FILES);

Link to comment
Share on other sites

Have you set the enctype of your form to "multipart/form-data"? Don't know whether that will help...

Link to comment
Share on other sites

yeah this is what my form looks like

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

Link to comment
Share on other sites

when doing print_r($_FILES); the output is the following...Array ( [file] => Array ( [name] => DSC_1496.jpg [type] => [tmp_name] => [error] => 1 => 0 ) )
UPLOAD_ERR_INI_SIZEValue: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
http://www.php.net/manual/en/features.file-upload.errors.php
Link to comment
Share on other sites

just had a look in my php.ini and this is what's in there

 ;Maximum size of POST data that PHP will accept.post_max_size = 8M

that's the default as of installing it, i've not edited it.edit: The .jpg files i'm testing it with don't go over 3MB.

Link to comment
Share on other sites

Well, I'm just telling you what the error says. I would trust the error. Make a file that has this:<?php phpinfo(); ?>Run it, and check which php.ini is being used, you can also check what the current running value of upload_max_filesize is on that page (I thought the default was 2MB, maybe I'm wrong).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...