0

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

 <h3>Extra profile information</h3>
 <table class="form-table">
     <tr>
     <th><label for="Upload">Upload</label></th>
     <td>
        <form action="" method="post">
        <input name="profile_photo[]" type="file"  value="" />
        <input name="profile_photo[]" type="file"  value="" />
        <input name="profile_photo[]" type="file"  value="" />
        <br/>
    </form>
     </td>
 </table>

I used to use $x=$_GET['profile_photo']; and echo $x; before upload to database and it return empty or null.

3
  • Check $_FILES array for count. Commented May 8, 2013 at 9:04
  • Shouldn't it be $x=$_POST['profile_photo']; ? Commented May 8, 2013 at 9:05
  • You need to add enctype="multipart/form-data" to your form tag too Commented May 8, 2013 at 9:05

5 Answers 5

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

USE THIS CODE

Sign up to request clarification or add additional context in comments.

1 Comment

USING $_FILES[] NOT $_GET[]
2
if (!empty($_FILES) && is_array($_FILES)) {

  // you have files
}

Comments

0

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.

1 Comment

It would be better to check the $name['error'] value each time rather than if the name is set.
0

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

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.