Jump to content

$_FILE['type'] validation issue


bugzy

Recommended Posts

Can't get validation to work

 

<?php

if ((($_FILES["uploaded_image"]["type"] != "image/gif")
|| ($_FILES["uploaded_image"]["type"] != "image/jpeg")
|| ($_FILES["uploaded_image"]["type"] != "image/png"))
&& ($_FILES["uploaded_image"]["error"] != 4))
{
	//not valid type
}
else
{
	//is valid type
}

?>

 

even if I'm choosing jpeg, png and gif it keeps on telling me that it is an invalid file. I tried to print_r each files that I'm uploading  and I'm pretty sure each file has the same value type as the conditions above..

 

 

Anyone?  :confused:

Link to comment
https://forums.phpfreaks.com/topic/265731-_filetype-validation-issue/
Share on other sites

Because you're using negative comparisons, you need to use &&, not || for those comparisons. Using the || operator, the conditional will evaluate to TRUE as soon as any of the conditions are met. You want it to evaluate to TRUE only if ALL of the conditions are met. IOW if the file is not a jpg, and the file is not a gif, and the file is not a png, then it isn't a valid type. You should check the error field separately, and a better way to do that is to check if it's a zero. If it's anything other than a zero, there was an error.

The problem, as I see it, is that the file will always either not be a jpeg or png or a gif. Since when its a png it will not be a gif or jpeg.

I was dealing with something similar yesterday. I think this should do the trick.

 

<?php

if (!($_FILES["uploaded_image"]["type"] == "image/gif" || $_FILES["uploaded_image"]["type"] == "image/jpeg" || $_FILES["uploaded_image"]["type"] == "image/png") && $_FILES["uploaded_image"]["error"] != "4")
{
	//not valid type
}
else
{
	//is valid type
}

?>

Because you're using negative comparisons, you need to use &&, not || for those comparisons. Using the || operator, the conditional will evaluate to TRUE as soon as any of the conditions are met. You want it to evaluate to TRUE only if ALL of the conditions are met. IOW if the file is not a jpg, and the file is not a gif, and the file is not a png, then it isn't a valid type. You should check the error field separately, and a better way to do that is to check if it's a zero. If it's anything other than a zero, there was an error.

 

Thanks again Pikachu2000  8)

The problem, as I see it, is that the file will always either not be a jpeg or png or a gif. Since when its a png it will not be a gif or jpeg.

I was dealing with something similar yesterday. I think this should do the trick.

 

<?php

if (!($_FILES["uploaded_image"]["type"] == "image/gif" || $_FILES["uploaded_image"]["type"] == "image/jpeg" || $_FILES["uploaded_image"]["type"] == "image/png") && $_FILES["uploaded_image"]["error"] != "4")
{
	//not valid type
}
else
{
	//is valid type
}

?>

 

 

GOAT 1st post :)

 

I tried it and it works  8)

 

 

Thanks!

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.