0

I have a multiple file types on my page. My issue is, if I start to upload the file or image from first then it's working.

enter image description here

and If I randomly upload the file then I am getting the error.

enter image description here

The is the full code.

HTML

<div class="container">
<form action="process.php" method="post" name="multiplefiles" enctype="multipart/form-data" id="myprofile">

    <div class="input_fields_wrap">
          <?php
          $labelname = array('Label 1','Label 2','Label 3','Label 4','Label 5','Label 6','Label 7');
          $labelCount=count($labelname);
            ?>
       <div class="row mt-3">
          <?php 
          for ($i=0; $i < $labelCount; $i++) { 
           ?>

           <div class="col-lg-4 mb-4 ">
             <div class="documentUploadWrap">
                <label><?php echo $labelname[$i];?></label>
             <div class="upload_doc">
              <input type="hidden" name="docUploadLabel[]" value="<?php echo $labelname[$i];?>">
              <input type="file" name="docUpload[]"  class="fileupload">
              <input type="hidden" name="docUpload[]" value="">

              <div class="uploadInfo">
                 <div class="upload_icon"></div>
                 <p>Drop your image here, or <span>browse</span></p> 
                 <span>Supports: JPEG, PNG, PPT, PPTX, PDF <br />Max Size: 5MB</span>
              </div>
              
           </div>

             </div>
          </div> 
        <?php } ?>

</div>
<input type="submit" name="send" value="Submit" class="btn btn-primary">
</div>
</form>
</div>

Process

if (isset($_POST['send'])) {
  
$total = count(array_filter($_FILES['docUpload']['name']));

for($i=0; $i < $total; $i++) {

if(isset($_FILES['docUpload']['name'][$i]) && $_FILES['docUpload']['name'][$i] != "")
{
    $foldername="uploads";
    $uploadLabel=$_POST['docUploadLabel'][$i];

      $image1  = $_FILES['docUpload']['name'][$i];
      $filename  = basename($image1);
      $onlyfile = pathinfo($filename, PATHINFO_FILENAME);
      $extension = pathinfo($filename, PATHINFO_EXTENSION);

      $file      = mt_rand();// random number 
      $newname   = $onlyfile.'_'.$file.'.'.$extension;
       
      echo $location='images/'.$foldername.'/'.$newname;

      if($extension=='png' || $extension=='jpg' || $extension=='jpeg') { 
        compressImage($_FILES['docUpload']['tmp_name'][$i],$location,60); 
      } 
      else{ 
        move_uploaded_file($_FILES['docUpload']['tmp_name'][$i], $location); 
      }
      $uploadDoc=$newname;


 }
 else{
   if (isset($_POST['docUpload'][$i]) && $_POST['docUpload'][$i]!= "") {
      $uploadLabel=$_POST['docUploadLabel'][$i];
      $uploadDoc=$_POST['docUpload'][$i];
    }
 }

$dataupload=array(
             'upload_label'=>$uploadLabel,
             'upload_name'=>$uploadDoc,
          );
 echo "<pre>";
 print_r($dataupload);
 
}

}

echo"<pre>"; var_dump($_FILES); output. I uploaded the image in label 4 and label 6

enter image description here

user9437856
  • 2,360
  • 2
  • 33
  • 92
  • Hello there, correct me if i wrong, so you upload the file randomly and not in order from label1 to label7 ? – Thomas Jeriko Jun 10 '21 at 02:20
  • @ThomasJeriko, Yes, The first I uploaded label 1 and then label 2 then it's working but if i upload first label 4 and then label7 then i am getting the error – user9437856 Jun 10 '21 at 02:22
  • I think there is some issue with array_filter which i am using. – user9437856 Jun 10 '21 at 02:23
  • did you try to var_dump or print_r the content of $_FILES ? var_dump($_FILES['docUpload]) see what inside – Thomas Jeriko Jun 10 '21 at 02:24
  • @ThomasJeriko, Yes, I tried echo"
    "; var_dump($_FILES); before the count and i am getting the correct name. I mean If I upload label 4 and label 7 then showing the name
    – user9437856 Jun 10 '21 at 02:27
  • @ThomasJeriko, I uploaded the var_dump($_FILES) output in the question. – user9437856 Jun 10 '21 at 02:31
  • so i recreate your code like this https://3v4l.org/dalJP, just remove "if" section on "else" section but keep the content inside – Thomas Jeriko Jun 10 '21 at 02:48
  • @ThomasJeriko, let me check the code. give me some time – user9437856 Jun 10 '21 at 02:50
  • @ThomasJeriko, I checked your answer, You have removed the array_filter. right? but if I remove the array_filter then I will get another issue, which Means If I submit the data into the database then I will get the empty value in the database – user9437856 Jun 10 '21 at 02:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233586/discussion-between-thomas-jeriko-and-user9437856). – Thomas Jeriko Jun 10 '21 at 02:55

2 Answers2

0

This happened because there double if with same condition, make the $uploadname and $uploadlable become undefined

<?php
$filedemo=array('docUpload'=>array(
    array('label'=>'Label1','file'=>null),
    array('label'=>'Label2','file'=>'file2'),
    array('label'=>'Label3','file'=>null),
    array('label'=>'Label4','file'=>'file4'),
    array('label'=>'Label5','file'=>null),
    array('label'=>'Label6','file'=>null),
    array('label'=>'Label7','file'=>null),
   )
);
$total = count($filedemo['docUpload']);

for($i=0; $i < $total; $i++) {

if(isset($filedemo['docUpload'][$i]['file']) && !empty($filedemo['docUpload'][$i]['file']))
{
    $uploadLabel=$filedemo['docUpload'][$i]['label'];
    $uploadDoc=$filedemo['docUpload'][$i]['file'];

}
else{
     $uploadLabel=$filedemo['docUpload'][$i]['label'];
     $uploadDoc=$filedemo['docUpload'][$i]['file'];
}

$dataupload=array(
     'upload_label'=>$uploadLabel,
     'upload_name'=>$uploadDoc,
  );
 echo "<pre>";
 print_r($dataupload);
}

Example :https://3v4l.org/dalJP

Thomas Jeriko
  • 213
  • 3
  • 13
0

A simple demonstration that array_filter() does not re-index the array after removing empty-ish values:

$array = ["1", "", "3", "", "4"];
var_export(array_filter($array));

Output:

array (
  0 => '1',
  2 => '3',
  4 => '4',
)

So, you see, if you use count() and for() (and I wouldn't) to iterate then PHP will complain that you are trying to access non-existent data at $i == 1.

Instead, forget using array_filter() -- which has a bad habit of over-filtering anyhow.

Just loop over the whole array with foreach() and check if the name string has length.

foreach ($_FILES['docUpload']['name'] as $name) {
    if (!strlen($name)) {
        continue;
    }
    // do the rest of your processing with the $name string
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136