Jump to content

[SOLVED] imagecreatefromjpeg() help!


Pickle

Recommended Posts

Hi everyone

 

ive been trying to create some secure validation for image upload i.e. ensuring that the file being uploaded is actually an image and nothing else by using imagecreatefromjpeg() and such.

 

Here is what i have however this seems to stop everything and im not sure why. any help would be much appreciated.

if($_FILES['thumb']['name'] != ""){ 

		$allowed_filetypes = array('.jpg','.gif','.jpeg','.png'); 
		$filename = $_FILES['file']['name'];
		$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 

		// Use the correct function for the filetype.
		switch (strtolower($ext)) {
			case '.gif':
				$im = @imagecreatefromgif($filename);
				break;
			case '.jpg':
			case '.jpeg':
				$im = @imagecreatefromjpeg($filename);
				break;
			case '.png':
				$im = @imagecreatefrompng($filename);
				break;
			default:
				$im = false;
		}

		if(!in_array($ext,$allowed_filetypes)){
		  	die('The file you attempted to upload is not allowed.');
		}elseif($im){// if $im is set then it must be a valid image of the indicated type.

			//upload file
			imagedestroy($im);

		}else{
			die('not an image.');
		}	
	}

 

thanks in advance

Link to comment
Share on other sites

Try this switch:

 

switch (strtolower($ext)) {
			case '.gif':
				$im = @imagecreatefromgif($filename);
				break;
			case '.jpg':
				$im = @imagecreatefromjpeg($filename);
				break;
			case '.png':
				$im = @imagecreatefrompng($filename);
				break;
			default:
				$im = false;
                                        break;
		}

 

You had a case 'jpg': option and it didn't have a break.

Link to comment
Share on other sites

Hi Alexwd

 

thanks for your help, one more question, is there another way of doing this? using the imagecreatefromjpeg() function to check that its an image was a suggestion to my by someone else. is there a better way or just another way of doing this?

 

i know theres also getimagesize() but none of these really guarantee thats its definitely an image right?

 

Link to comment
Share on other sites

When validating images I always use getimagesize() it works fine and you don't have to create that annoying switch for different image types. It also already gives you access to other information about that file that you may need after you've confirmed it's valid.

Link to comment
Share on other sites

am i right in saying that you can use getimagesize() before the file is uploaded because obviously this is what i need, to make sure its ok before i upload it.

 

so basically i would just say the following:

$filename = $_files['file']['name'];
$info = getimagesize($filename);

if(isset($info)){
   //upload file here
}

 

is that right? that i should check if the variable $info is set? or should i be checking for a specific size?

Link to comment
Share on other sites

$_FILES['file']['name'] gives the filename that was on your specific computer I believe (IE if you uploaded myname.jpg, than that would return myname.jpg) the problem is that the particular file myname.jpg doesn't exist on your server until you move it from the temp folder (assuming you move it with the same name as it had).

 

$_FILES['file]['temp'] gives the file name of your file in the temporary folder I think, so you may be able to do it with that.

Link to comment
Share on other sites

so are you saying that it creates a temp file when the form is submitted? i thought that it only did this once you went ahead with the image upload.

 

so i need to be saying

$temp_loc = $files['file']['temp'] ;
$info = getimagesize($temp_loc);

 

thanks again

Link to comment
Share on other sites

so i suppose thats the same with what i was trying to do before?

 

$filename = $_FILES['file']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
         
         // Use the correct function for the filetype.
         switch (strtolower($ext)) {
            case '.gif':
               $im = @imagecreatefromgif($filename);
               break;
            case '.jpg':
            case '.jpeg':
               $im = @imagecreatefromjpeg($filename);
               break;
            case '.png':
               $im = @imagecreatefrompng($filename);
               break;
            default:
               $im = false;
         }

 

but

$filename = $_FILES['file']['name'];

 

should be

 

$filename = $_FILES['file']['temp'];

 

thats why it wasnt working?! :D

Link to comment
Share on other sites

hi

 

i still cant seem to get this to work. if my form element is called image i.e.

 

<input type='file' name='image' value='' />

 

then should i be saying:

 

$temp_file = $_FILES['[b]image[/b]']['temp'];
$info = getimagesize($temp_file);

 

or

 

$temp_file = $_FILES['[b]file[/b]']['temp'];
$info = getimagesize($temp_file);

 

which ever i use anyway it always says that its not an image. not sure what im doing wrong, any help would be great.

 

thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.