Victor Adewale Victor Adewale - 1 year ago 74
PHP Question

Upload two different files in the same form

The issue here is, when I Upload videos, it works perfectly but not images which is giving me a hard time to figure out what the error may be

This is what the HTML looks like:

<form action="index.php" method="post" enctype="multipart/form-data" >
<h1>Browse for file to upload:</h1>
<label for="file"><h1><b>Select Video</b></h1></label>
<input type="file" name="fileToUpload1[]" id="fileToUpload" >


<label for="file"><h2><b>Select Image</b></h2></label>
<input type="file" name="fileToUpload1[]" id="fileToUpload" >
<input type="submit" value="Upload" name="submit">

The PHP Script looks like this:

$error = null;
$success = null;
$info = [];
$uploadOk = 1;

$allowedMimes = ['video/mp4', 'image/jpg'];

if(isset($_POST["submit"])) {

$counter = 0;
foreach ( $_FILES['fileToUpload1']['name'] as $file ):
$target_file = UPLOAD_DIR . basename($file);
if ( in_array(mime_content_type($_FILES['fileToUpload1']["tmp_name"][$counter]), $allowedMimes) ){
if (move_uploaded_file($_FILES['fileToUpload1']["tmp_name"][$counter], $target_file)) {
$info[] = "File ".++$counter."($file) uploaded successfully";
}else {
$info[] = "File ".++$counter."($file) cannot be uploaded";
} else {
$info[] = "File ".++$counter. " is not allowed.";

} else {
echo "upload a file.";


<?php foreach($info as $i){ ?>
<?php }?>
<? } ?>

Answer Source

The mimetype of jpg files is actually image/jpeg (and not iamge/jpg), so if you are trying to upload jpg files you should change your $allowedMimes variable to:

$allowedMimes = ['video/mp4', 'image/jpeg'];

Also - if you want to support other image types you can use:

$allowedMimes = ['video/mp4', 'image/png', 'image/jpeg', 'image/gif', 'image/bmp'];

Another option - if you want to support all image/audio/video types you can check only the first part of the string:

$allowedTypes = ['video', 'image', 'audio'];
if ( in_array(
    $allowedTypes) )

If you are using php>=5.4 you can use:

$allowedTypes = ['video', 'image', 'audio'];
if ( in_array(
    $allowedTypes) )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download