Jump to content

Test for image type


Recommended Posts

Want to test that user uploads right type of file. It correcly does not move the file if incorrect type, but the error message does always display "'Image 1' is a required field" instead of showing "'Image 1' accepts only jpg and gif" when wrong file type is chosen. The relevant code is as follows:

$allowed = array ('image/gif', 'image/jpeg');if (isset($_POST['submitted'])) { // handle the form//initialise error array	$errors = array();	// check for img1	if (!isset($_POST['img1']) OR empty($_POST['img1'])) {		$img1 = FALSE;		$errors['img1'] = '\'Image 1\' is a required field';		} else {		if (!in_array($allowed, $_POST['img1']))  {		$errors['img1'] = '\'Image 1\' accepts only jpg and gif';		$img1 = FALSE;		} else {		$img1 = TRUE;		}		}	if (isset($_FILES['img1'])) { 		var_dump ($errors);	// validate the input	if (in_array($_FILES['img1']['type'], $allowed)) {			// move the file over			if(move_uploaded_file($_FILES['img1']['tmp_name'], "$images/{$_FILES['img1']['name']}")) {

Tried several ways of doing the check for img1, but always same result...Kurt

Link to post
Share on other sites

Hi, as far as i know, inside the $_POST there is not information of the type of the file.I found this in your code:this is the line where you're going to get a valid/invalid type, something like this:

if (in_array($_FILES['img1']['type'], $allowed)){/* *more code */}else{$errors['img1'] = '\'Image 1\' accepts only jpg and gif';}

hope this helps.

Link to post
Share on other sites
Hi, as far as i know, inside the $_POST there is not information of the type of the file.I found this in your code:this is the line where you're going to get a valid/invalid type, something like this:
if (in_array($_FILES['img1']['type'], $allowed)){/* *more code */}else{$errors['img1'] = '\'Image 1\' accepts only jpg and gif';}

hope this helps.

Thanx. Had a go, but won't do the trick. I change the field validation now to:
	if (!isset($_POST['img1']) OR empty($_POST['img1'])) {		$img1 = FALSE;		$errors['img1'] = '\'Image 1\' is a required field';		} else {		if (in_array($_FILES['img1']['type'], $allowed)){		$img1 = TRUE;		} else {		$errors['img1'] = '\'Image 1\' accepts only jpg and gif';		$img1 = FALSE;		}		}

it still only shows the first error message and this not only when I upload the wrong data type, but also when the upload went ok (just then it does not display the general: could you please amend the highlighted fields bit, which I have further down in the code).I used a similar validation on text inputs and worked well. Obviously, file uploads are different, but I cannot see what should now be wrong here?Kurt

Link to post
Share on other sites

Can we see your HTML form?File uploads are sent in the $_FILES array rather than the $_POST array; see http://us3.php.net/manual/en/features.file-upload.php. A file will not be found in $_POST, even though the form method may be "post". So this:

if (!isset($_POST['img1']) OR empty($_POST['img1'])) {

should be this:

if (!isset($_FILES['img1']) OR empty($_FILES['img1'])) {

Link to post
Share on other sites

from the manual:

$_FILES['userfile']['type'] The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
As a better alternative, check out the finfo_file() and related routines described on the finfo_file() page. BUT, depending on your security, be sure to scroll down the page and read the comment by Schraalhans Keukenmeester.
Link to post
Share on other sites

Had a read through the given sites and changed my file to:

	if (!isset($_FILES['img1']) OR empty($_FILES['img1'])) {				$errors['img1'] = '\'Image 1\' is a required field';		$img1 = FALSE;		} else {		if (!in_array($_FILES['img1']['type'], $allowed)){		$errors['img1'] = '\'Image 1\' accepts only jpg and gif';		$img1 = FALSE;		} else {		$img1 = TRUE;		}		}

with the form being:

<form action="uploadTest.php" enctype="multipart/form-data" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="524288" />  <fieldset>  <table>  <tr><td colspan="2"><label for="img1"><?php echo check_error('img1', 'Image 1*'); ?></label><br /><input type="file" name="img1" size="40" maxlength="10" value="img1" /></td></tr>  <tr><td colspan="2"><input type="submit" name="submit" value="Upload your data" /><input type="hidden" name="submitted" value="TRUE" /></td></tr>  </table>  </fieldset></form>

Still not working (with the second error message displayed for wrong type or empty field) and I also noted that also I have my image type array as:$allowed = array ('image/gif', 'image/jpeg', 'image/jpg');I am actually only able to upload gifs. Now it is getting really confusing for me...Deirdre's Dad, will look a bit more into your suggestion once I made my simple script work. My usual weakness is to start running before I can even walk;-) This time I take it one step at a time...Kurt

Link to post
Share on other sites
IE sends the jpeg type as image/pjpeg. You might want to check the file extension instead.
I amended the type array to:$allowed = array ('image/gif', 'image/jpeg', 'image/pjpeg', 'image/jpg');and it does the trick. But this is not what you meant, is it? Do you mean sth as:
if(!function_exists('image_type_to_extension')){   function image_type_to_extension($imagetype)   {       if(empty($imagetype)) return false;       switch($imagetype)       {           case IMAGETYPE_GIF    : return 'gif';           case IMAGETYPE_JPEG    : return 'jpg';           case IMAGETYPE_PNG    : return 'png';                      default                : return false;       }   }}

If yes, where and when would I call this function?Also, the error message get displayed now correctly. I amended the first line to read:if (!isset($_FILES['img1']['name']) OR empty($_FILES['img1']['name'])) {and it solved the issue...KurtReason for edit: I actually just saw the other problem the image type caused. I name the file as:

$type = $_FILES['img1']['type'];$ext = substr ($type, 6);$img1 = $user_id . '-1.' . $ext;

Now I have got file named 16.pjpeg for example. This is not what I want. It would really be great if you could point me with the other approach in right direction...

Link to post
Share on other sites

Leave out the type, don't use it. Use only the file extension. Converting to an extension from a type is still using the type. Get the extension from $_FILES['img1']['name'], you can use this:$ext = array_pop(explode(".", $_FILES['img1']['name']));Once you have the extension then you can check if it is "jpg" or whatever you want. You can also use $ext when you rename the file so it keeps the original extension.

Link to post
Share on other sites
Leave out the type, don't use it. Use only the file extension. Converting to an extension from a type is still using the type. Get the extension from $_FILES['img1']['name'], you can use this:$ext = array_pop(explode(".", $_FILES['img1']['name']));Once you have the extension then you can check if it is "jpg" or whatever you want. You can also use $ext when you rename the file so it keeps the original extension.
Use now:
		$ext = explode('.',$_FILES['img1']['name']);		$ext = $ext[count($ext)-1];

which seems to do the trick... Thank you!Kurt

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...