Jump to content

detecting uploaded file type and error checking


Lodius2000

Recommended Posts

my script is designed to display errors and redisplay the form if any are triggered, instead of processing the form, I use this architecture all the time and it is sound, so given the following code

 

if ($_FILES['uploadfile']['type'] != 'image/jpeg'){

$errors[] = 'Please upload an image with an extension of .jpg or .jpeg';

}

 

 

when i upload a tiff it should detect a non jpg file and display the error and redisplay the form, instead it tries to process the form and resize and all the other crap i have it do with the uploaded file, and since it is a tiff, imagecreatefromjpg() doesnt really work all that well

 

is there something wrong with the conditions of my if() , i have echoed out $_FILES['uploadfile']['type'] and it tells me the right type

cooldude

 

so i just need to adjust the $data integer to whichever type i want so for jpg i would put $data[1] in the function

 

also could i define the function earlier and then make another if() that says

 

if (! is_img){

//print error

}

 

example use

<?php
is_img($img_path){
$image_types = array("image/jpeg", "image/jpg", "image/pjpg", "image/pjpeg", "image/gif", "image/pgif", "image/png", "image/ppng");
$data = getimagesize($img_path);
if(in_array($data[3],$image_types)){
	return TRUE;
}
else{
	return FALSE;
}
}

#assume the function is in this file (the is_img function I wrote)
$img = new Img_Class
if(is_img($img->path)){
#its good do what you want
}
else{
#use your custom class error reporting
$img->error_report("Its a broken image");
}
?>

 

 

make sense

kinda but i dont know oop

 

but to fit into my architecture i am pretty sure this line

 

if(is_img($img->path)){

 

must return false, because if it were true it wont throw an error, i have no need of an else clause because all my errors tests if something is false, at that point they throw an error and redisplay the form

 

so can I use if(! is_img($img->path)){

 

or should i change the functions if() to read

 

if(! in_array($data[3],$image_types)){

 

also for $img_path, do I use $_FILES['uploadfile']['name'] or am i really not understanding this

Archived

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

×
×
  • 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.