0

in my form I'm trying to upload multiple or single file using php. So my html table is look like this :

<tr>
<td width="142"><b>Docs</b>
<td width="142"><input type="file" name="files[]" id="project_docs1" class="docfile" /></td>
<td width="142"><input type="file" name="files[]" id="project_docs2" class="docfile" /></td>
<td width="142"><input type="file" name="files[]" id="project_docs3" class="docfile" /></td>
</td>

Now when I upload only one file it's showing me error message like : Invalid Format but it's should be accept one file. it's not require to must be upload all 3 files. Can you tell me why it's showing this error message called Invalid Format ? If upload all 3 files then it's working fine.

and when I press the upload button without upload any file it's showing me value 1 for $noOfUpload variable. why ?

$valid_formats = array("jpg", "png", "gif", "txt", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "project_docs"; // Upload directory
$error =  array();  // load error message
$files_name =  array(); // get uploaded file name

foreach ($_FILES['files']['name'] as $key => $name) {

    $size = $_FILES['files']['size'][$key]. "<br>"; 
    $noOfUpload = count($name);

    if($noOfUpload <= 0){
        $error[] =  "Upload your document/s<br>";
    }elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
        $error[] = "invalid file formate : $name<br>";

    }

    //$name = md5(uniqid()) . '-' . htmlspecialchars_decode($name);
    $files_name[] = "$name";

}

if(!empty($error)){
    foreach ($error as $e) {
        echo "<div class='error'>$e</div>";
    }   
}else{
    foreach ($files_name as $fn) {  
        echo "$fn<br>";
    }       
}

You help is more appreciate. :)

3 Answers 3

1

Try this

    if (isset($_FILES))
    {
        $valid_formats = array("jpg", "png", "gif", "txt", "bmp");
        for ($i = 0; $i < count($_FILES['files']['name']); $i++)
        {
           if (in_array(pathinfo($FILES['files']['name'][$i], PATHINFO_EXTENSION), $valid_formats))
          {
             $tmp_path = $_FILES['files']['tmp_name'][$i];
             if ($tmp_path != "")
             {
                 if (move_uploaded_file($tmp_path, $new_path))
                 {
                     //Handle other code here
                 }
                 else
                 {
                     $error[] = ""; //your error handling
                 }
             }
             else
             {
                 $error[] = ""; //your error handling
             }
         }
         else
         {
             $error[] = "invalid file formate : $name<br>";
         }
     }
 }
Sign up to request clarification or add additional context in comments.

7 Comments

Ok, I'm trying with this.
@creativeartbd please check the edit, $_FILES['files'] as $file
@creativeartbd the way you are using multiple uploads will return different type of array, my above given code will work for <input type='file' name='files[]' multiple='multiple' />... by using this you can select multiple files in a single file selector by using the ctrl key.. and than use my above given code.
why this line of code is true : $file = $_FILES['files']['name']; if(isset($file) && !empty($file)){ echo "hello file"; }else{ echo "no file"; }
it's showing "hello file" (for example) without upload any file.
|
0

Your code is run for whole array either it's blank or not. First, count your array then run your code upto that count.

2 Comments

if he doesnt select the file than also the count of the array will be 3, he have to check the name or other variables of the file is set or not..
if he doesn't select the file than the count of an array will be 0. like $array_count = $_FILES['files'][$i]['name']; count(array_filter($array_count)); Here $i is total count of files array
0
    <tr>
     <td width="142"><b>Docs</b>
     <td width="142"><input type="file" name="files[]" id="project_docs1" class="docfile" /></td>
    </tr>

PHP Code:

    $valid_formats = array("jpg", "png", "gif", "txt", "bmp");
    $max_file_size = 1024*100; //100 kb
    $path = "project_docs"; // Upload directory
    $error =  array();  // load error message
    $files_name =  array(); // get uploaded file name

    foreach ($_FILES['files']['name'] as $key => $name) {

    $size = $_FILES['files']['size'][$key]. "<br>"; 
    $noOfUpload = count($name);

    if($noOfUpload <= 0){
        $error[] =  "Upload your document/s<br>";
    }elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){
        $error[] = "invalid file format : $name<br>";

    }

    //$name = md5(uniqid()) . '-' . htmlspecialchars_decode($name);
    $files_name[] = "$name";

}

if(!empty($error)){
    foreach ($error as $e) {
        echo "<div class='error'>$e</div>";
    }   
}else{
    foreach ($files_name as $fn) {  
        echo "$fn<br>";
    }       
}

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.