Check Upload file type (multiple)

I would like to upload multiple files(image)
How can I check if a file extension and mime type are in an array this is the code I currently have.

This is my index code…


<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="file" name="image2" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="submit" id="submit" value="Upload" />
</form>

and this is my upload.php code.


function is_valid_type($file)
{
	// This is an array that holds all the valid image MIME types
	$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif","image/png");

foreach ($_FILES['image'] as $file) {

	if (!in_array($file['type'], $valid_types))
		return 1;
	return 0;
}
}


To upload multiple files you will need to add/change this line to:


/* add more if needed */
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />
<input type="file" name="image[]" /><br />

Change this:


foreach ($_FILES['image'] as $file) {

    if (!in_array($file['type'], $valid_types))
        return 1;
    return 0;
}

To this:


foreach ($_FILES['image']['type'] as $file) {

    if (!in_array($file, $valid_types))
        return 1;
    return 0;
}

Thank spence_noodle for replaying here.
I am getting this error :
Warning: Invalid argument supplied for foreach() in

Ok I am storing image names to database
image…image5 like that give any idea.

Ok, you will need to do a for loop instead and check to see if the file type is correct:


// count how many files have been used
$count = count($_FILES['photo_filename']['name']);

function Name() {
    // now loop through the number of files used
    for($i = 0; $i < ($count); $i++) {

     // check the file type is allowed, if so continue
     if(in_array($_FILES['photo_filename']['type'][$i], $vaild_types)) 
     {
        // do what ever here
     }

  }
}

Not working…

when i use below code that working for ist image check correctly and 2nd one does not check correctly
this is my code… please check my below code any one.


// Check to see if the type of file uploaded is a valid image type

function is_valid_type($file)
{
	// This is an array that holds all the valid image MIME types
	$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif","image/png");

	if (in_array($file['type'], $valid_types))
		return 1;
	return 0;
}




// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
	echo "<pre>";
	print_r($array);
	echo "</pre>";
}


How about this:


<?php
// count how many files have been used
$count = count($_FILES['image']['name']);

function Name($count)
{
	$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");
  
	// now loop through the number of files used
	for ($i = 0; $i < ($count); $i++)
	{
		// check the file type is allowed, if so continue
		if (in_array($_FILES['image']['type'][$i], $valid_types))
		{
			echo "ok";
		}
		else
		{	
			echo "nope";
		}
	}
}

Name($count)

?>

<form action="test.php" method="post" enctype="multipart/form-data"> 
File_1: <input type="file" name="image[]" /><br />
File_2: <input type="file" name="image[]" /><br />
File_3: <input type="file" name="image[]" /><br />
File_4: <input type="file" name="image[]" /><br />
File_5: <input type="submit" id="submit" value="Upload" />
</form>

Thank U mate.
all files uploading and also doesn’t writing files name in database

In above post all files Means (.php, .html etc) files are uploading.

I tried this code for 3 days i didn’t find error. one more thing when I uploading a single image that works fine but not for two files. that means i used 2 inputs

please solve my problem

thank u once again.

Well ignoring for the moment that you forgot the ; on the end of Name($count)…
What does Ernie’s code put out for you?

than u StarLion respond here.

this is my code when updated with help up Ernie’s

index.html


<form action="upload.php" method="post" enctype="multipart/form-data"> 

File_1: <input type="file" name="image[]" /><br />

File_2: <input type="file" name="image[]" /><br />


 <input type="submit" id="submit" value="Upload" />

</form>

upload.php code


<?php
// Start a session for error reporting
session_start();

// Call our connection file
require("includes/conn.php");

// Check to see if the type of file uploaded is a valid image type

$count = count($_FILES['image']['name']);


function is_valid_type($count)
{
    // This is an array that holds all the valid image MIME types
    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif","image/png");

    // now loop through the number of files used
	
    for ($i = 0; $i < ($count); $i++)
  
{

        	// check the file type is allowed, if so continue
        	if (in_array($_FILES['image']['type'][$i],$valid_types))
			//if (in_array($image['type'][$i], $valid_types))
			{
        return 1;
		}

        else
		{ 
    return 0;

}
}
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
    echo "<pre>";
    print_r($array);
    echo "</pre>";
}








// This variable is the path to the image folder where all the images are going to be stored
$target = "images/";
$targetx = $target . basename( $_FILES['image']['name'][$i]);



// Get our POSTed variables
$image = $_FILES['image'];
$image = $_FILES['image'];



// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$targetx = $target . basename( $_FILES['image']['name'][0]);
$targetx = $target . basename( $_FILES['image']['name'][1]);




// Make sure all the fields from the form have inputs
if (  $image == ""  )
{
    echo "All fields are required";
    echo "<br/>";
    echo '<a href="1.php">Back</a>';
    exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
    echo "You must upload a jpeg, gif, or bmp";
    echo "<br/>";
    echo '<a href="1.php">Back</a>';
    exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($targetx))
{
    echo "A file with that name already exists";
    echo "<br/>";
    echo '<a href="1.php">Back</a>';
    exit;
}




$targetx = $target . basename( $_FILES['image']['name'][0]);

//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'][0], $targetx))
{

//Tells you if its all ok
echo "Image1: Uploaded Sucess!";
echo "<br/>";
}
else {
//Gives and error if its not
echo "Image1: Invalid!";
echo "<br/>";

}

$targetx = $target . basename( $_FILES['image']['name'][1]);

//Writes the photo to the server
if(move_uploaded_file($_FILES['image']['tmp_name'][1], $targetx))
{

//Tells you if its all ok
echo "Image2: Uploaded Sucess!";
}
else {
//Gives and error if its not
echo "Image2: Invalid!";
}
echo "</br>";
    
    
?> 

this is actually just like multiple image upload.
I would like to upload only image files only. in my code uploading all types of files.

Okay. I see the problem you’re having.
RETURN immediately ends all processing on the function (except cleanup, obviously).
You dont want to return where you’re returning.
Try this instead.


function is_valid_type() 
{ 
    // This is an array that holds all the valid image MIME types 
    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif","image/png"); 

    // now loop through the number of files used     
    foreach($_FILES['image']['type'] AS $type) {
            if (!in_array($type,$valid_types)) {
               return 0;
            }
    }
    return 1;
} 

Wow… Amazing… Its working

Thank U very very… much… StarLion

function is_valid_type($file)
{
// This is an array that holds all the valid image MIME types
$valid_types = array(“image/jpg”, “image/jpeg”, “image/bmp”, “image/gif”,“image/png”);

foreach ($_FILES[‘image’] as $file) {

if (!in_array($file['type'], $valid_types))
	return 1;
return 0;

}
}