Jump to content

Recommended Posts

I have a form with 5 file fields.. if any of those fields contain a file type thats no allowed, the populate $error_msg, if all the files are correct file types, them leave $error_msg empty..

 

I have been trying with the likes of below, but it apears to stop all file uploads even when all the files are jpg..

 

$types_array = array("image/jpeg","image/pjpeg");
if (!in_array($_FILES['photoname']['type'], $types_array)) {
$error_msg = 'Invalid File type';
}

 

?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/268199-file-extension-in_array/
Share on other sites

The [type] in $_FILES is not secure. You can't trust it to tell you what type of file was uploaded.

 

For images, use getimagesize() on the [tmp_name]. If it errors then the file isn't a recognizable image, otherwise it'll tell you anything you need to know about it - including the type of image.

$types_array = array(IMAGETYPE_JPEG, IMAGETYPE_ANOTHERIMAGE);

 

That is simple, replace your array code with the above (change the ANOTHERIMAGE to a valid type) and then:

 

if (!in_array(exif_imagetype('image.gif'), $types_array)) {

its checking the 5 files that were sent $_FILES['photoname']..

 

I see, I guess I was mislead by the code you showed, as you seemed to only try 1 file. Do you have more code to provide so I can more accurately assist you with what you want to achieve? Namely the loop portion of the code that iterates over the Photos.

A basic cut down version of what I am trying to acheive..

 


if(isset($submit)){	

if(any_of_the_5_photos_are_not_jpeg) {
     $error_msg = 'Only jpg files allowed';
}

// Insert record

}

// show form again if error message is not null
if (($error_msg == '') && (isset($submit))){


<form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data">

$number_of_fields = 5;
$counter = 1;

while($counter <= $number_of_fields){
echo '<input name="photoname[]" type="file" id="upload" />';
  	$counter++;
}

echo '<input type="submit" name="submit" id="submit" value="submit" />';

</form>

}

 

Thanks

<?php
//... code above this
   $types_array = array(IMAGETYPE_JPEG, IMAGETYPE_ANOTHERIMAGE);

    $badImg = array();
    foreach ($_FILES['photoname']['tmp_name'] as $key => $iData) {
         if (!in_array(exif_imagetype($iData), $type_array)) {
               $badImg[] = $key;
         }
    }

    if (empty($badImg)) {
          // All files passed. 
    } else {
          // there was an error and ids are in $badImg
    }

 

Something like that should get you going.

I think is what is being missed here is that there are apparently multiple file input fields. I would assume that not all of them are required. But, the post/file data will include records for all the fields - even if they have no data. So, the logic needs to exclude fields where a file was not supplied.

 

My take:

$valid_types = array(IMAGETYPE_JPEG);

//Check if any files were submitted
if(isset($_FILES['photoname']))
{
    //Set flag variable
    $allFilesValid = true;
    //Iterate through all files submitted
    foreach($_FILES['photoname']['tmp_name'] as $tmp_file)
    {
        //If any "submitted" file is not valid set flag to false and exit loop
        if($tmp_file != '' && !in_array(exif_imagetype($tmp_file), $valid_types))
        {
            $allFilesValid = false;
            break;
        }
    }
}

Hey,

 

All file fields must hold files to submit.. I use the following to make sure all 5 file fields are populated..

 

if (array_sum($_FILES['photoname']['error']) > 0) {
    $error_msg = 'You must upload 5 logos to continue'; 
}

 

Thanks[/code]

I tested the following code:

 

$types_array = array(IMAGETYPE_JPEG, IMAGETYPE_ANOTHERIMAGE);

    $badImg = array();
    foreach ($_FILES['photoname']['tmp_name'] as $key => $iData) {
         if (!in_array(exif_imagetype($iData), $type_array)) {
               $badImg[] = $key;
         }
    }

    if (empty($badImg)) {
          // All files passed. 
    } else {
          // there was an error and ids are in $badImg
    }

 

Got the following error, which is the error I got no matter what I tried before posting..

 

Warning: in_array() [function.in-array]: Wrong datatype for second argument in 

/home/###/public_html/includes/forms.php(2) : eval()'d code(1) : eval()'d code

(1) : eval()'d code on line 35

 

Thanks

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.