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

Link to comment
Share on other sites

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
}

?>

Link to comment
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.

 

Thanks again Pikachu2000  8)

Link to comment
Share on other sites

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!

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.