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;
}
}
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
}
}
}
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>";
}
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
<?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;
}
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;