Skip to content Skip to sidebar Skip to footer

How Can I Check If Input Array Filed Type File Empty Doesn't Upload

How can i check array input type file is empty before upload data in database. if have one empty show anything that user understand and don't updata data. This is example code html

Solution 1:

$x=$_FILES['profile_photo']['name']; 

USE THIS CODE


Solution 2:

if (!empty($_FILES) && is_array($_FILES)) {

  // you have files
}

Solution 3:

Just run a for loop on the post data and verify

$error = "";
$emptyFiles="";
$i=0;
foreach ($_FILES['profile_photo'] as $name) {
   if (isset($name['name']) && $name['name']!="") {
       $error.="";
   } else {
       $error.= "yes";
       $emptyFiles.=$_FILES['profile_photo'][$i];
   }
    $i++;
}

Then use

$error and $emptyFiles string to get which are empty fields and which are not.


Solution 4:

You can also validate inputs by javascript before submission

html:

<form action="" method="post" onsubmit="return validateInputs();">
    <input name="profile_photo[]" type="file"  value="" />
    <input name="profile_photo[]" type="file"  value="" />
    <input name="profile_photo[]" type="file"  value="" />
    <input type="submit" />
</form>

javascript:

validateInputs = function() {    
    inputs = document.getElementsByName('profile_photo[]');
    for (var ind in inputs){
        if (inputs[ind].value=="") {
            alert("validation failed");
            return false;
        }
    }         
}

Solution 5:

in javascript/jquery

var file = $(".profile_photo").map(function(){return $(this).val();}).get();


for(var i=0; i<file.length; i++){
   if( file[i] == ""){
     alert("Please select file");
     return false;
   }
}

in PHP

if( $_FILES['profile_photo']['error'] != 0 ){
   // code to handle if there is no file selected
}

Post a Comment for "How Can I Check If Input Array Filed Type File Empty Doesn't Upload"