Skip to content
Advertisement

PHP How to update uploaded multiple file in different column in DB table

I have an HTML form to upload two PDF files. Am able to successfully make it upload one file to a column but not sure how to upload each PDF files to a different column on the database table. Below is my code:

   if(isset($_POST["submit"])){
                 
$errors = array();
$uploadedFiles = array();
$extension = array("pdf");
$bytes = 5024;
$KB = 5024;
$totalBytes = $bytes * $KB;
$UploadFolder = "UploadFolder";
$counter = 0;


foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name){
  $temp = $_FILES["files"]["tmp_name"][$key];
  $vendor_blicense = $_FILES["files"]["name"][$key];
   
  if(empty($temp))
  {
      break;
  }
   
  $counter++;
  $UploadOk = true;
   
  if($_FILES["files"]["size"][$key] > $totalBytes)
  {
      $UploadOk = false;
      array_push($errors, $vendor_blicense." file size is larger than the 5 MB.");
    echo '<script>alert("File size is larger than the 5 MB...")</script>';
  }
   
  $ext = pathinfo($vendor_blicense, PATHINFO_EXTENSION);
  if(in_array($ext, $extension) == false){
      $UploadOk = false;
      array_push($errors, $vendor_blicense." is invalid file type. Only PDF");
      echo '<script>alert("Invalid file type. Only pdf")</script>';
  }
   
  
   
  if($UploadOk == true){

if(file_exists($UploadFolder."/".$vendor_blicense) == true){
 
$vendor_blicense1 = uniqid().$vendor_blicense;
$success = mysqli_query($kone,$query);


$preparedStatement = $kone->prepare("UPDATE table SET vendor_blicense = :vendor_blicense WHERE vendor_id = '$send_id'");

$preparedStatement->execute([ ':vendor_blicense' => $vendor_blicense1 ]);



        move_uploaded_file($temp,$UploadFolder."/".$vendor_blicense1);
      array_push($uploadedFiles, $vendor_blicense1);

      }


    else{

$preparedStatement = $kone->prepare("UPDATE table SET vendor_blicense = :vendor_blicense WHERE vendor_id = '$send_id'");

$preparedStatement->execute([ ':vendor_blicense' => $vendor_blicense ]);


      move_uploaded_file($temp,$UploadFolder."/".$vendor_blicense);
      array_push($uploadedFiles, $vendor_blicense);

    }
  }
}

     }


?>

<input type="file" id="files" name="files[]" accept=".pdf">
<input type="file" id="files2" name="files[]" accept=".pdf">
<input name="submit" type="submit" value="Submit">
“`

How can I get the name and PDF files of each input “file” and “files2” and submit to the different column on the database and Uploadfolder on the server? I have a query like this: UPDATE hobber_vendor SET vendor_blicense = :vendor_blicense AND vendor_btax = :vendor_btax WHERE vendor_id = ‘$send_id’ I want to update vendor_btax to store the name of files from “files2″ and also upload the file to move_uploaded_file($temp,$UploadFolder.”/”.$vendor_btax); array_push($uploadedFiles, $vendor_tax);

Advertisement

Answer

So after all research, troubleshooting, and following every line of my code. Was able to resolve this by firstly changing my HTML code to below:

<input type="file" id="files" name="files['license']" accept=".pdf">
<input type="file" id="files2" name="files['tax']" accept=".pdf">
<input name="submit" type="submit" value="Submit">


Then I got value for each input and assign it to each column on the database. Also, I declared the uniqid() outside my foreach loop to reuse in case users try to upload files that have the same path name as the one on the server. Below is my complete PHP code:

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

$errors = array();
$uploadedFiles = array();
$extension = array("pdf");
$bytes = 5024;
$KB = 5024;
$totalBytes = $bytes * $KB;
$UploadFolder = "UploadFolder";
$random = uniqid();

foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name){
  $temp = $_FILES["files"]["tmp_name"][$key];
  $filer = $_FILES["files"]["name"][$key];

  $vendor_blicense = $_FILES["files"]["name"]["license"];

  $vendor_btax = $_FILES["files"]["name"]["tax"];

if(empty($temp))
  {
      break;
  }
   
  $counter++;
  $UploadOk = true;
   
  if($_FILES["files"]["size"]["license"] > $totalBytes)
  {
      $UploadOk = false;
      array_push($errors, $vendor_blicense." file size is larger than the 5 MB.");
    echo '<script>alert("File size is larger than the 5 MB...")</script>';
  }
  
  if($_FILES["files"]["size"]["tax"] > $totalBytes)
  {
      $UploadOk = false;
      array_push($errors, $vendor_btax." file size is larger than the 5 MB.");
    echo '<script>alert("File size is larger than the 5 MB...")</script>';
  }
  

  $ext = pathinfo($vendor_blicense, PATHINFO_EXTENSION);
  if(in_array($ext, $extension) == false){
      $UploadOk = false;
      array_push($errors, $vendor_blicense." is invalid file type. Only PDF");
      echo '<script>alert("Invalid file type. Only pdf")</script>';

  }
  
  $ext = pathinfo($vendor_btax, PATHINFO_EXTENSION);
  if(in_array($ext, $extension) == false){
      $UploadOk = false;
      array_push($errors, $vendor_btax." is invalid file type. Only PDF");
      echo '<script>alert("Invalid file type. Only pdf")</script>';

  }
   
  
   
  if($UploadOk == true){

if(file_exists($UploadFolder."/".$filer)){

$filer1 = $random.$filer;
$vendor_blicense1 = $random.$vendor_blicense;
$vendor_btax1 = $random.$vendor_btax;
$success = mysqli_query($kone,$query);


$preparedStatement = $kone->prepare("UPDATE table SET vendor_blicense = :vendor_blicense, vendor_btax = :vendor_btax, vendor_tax_check = :vendor_tax_check,vendor_bstage = :vendor_bstage  WHERE vendor_id = '$send_id'");
$preparedStatement->execute([ ':vendor_blicense' => $vendor_blicense1, ':vendor_btax' => $vendor_btax1, ':vendor_tax_check' => $vendor_tax_check, ':vendor_bstage' => $vendor_bstage]);



        move_uploaded_file($temp,$UploadFolder."/".$filer1);
      array_push($uploadedFiles, $filer1);

      }


    else{

$preparedStatement = $kone->prepare("UPDATE table SET vendor_blicense = :vendor_blicense, vendor_btax = :vendor_btax, vendor_tax_check = :vendor_tax_check, vendor_bstage = :vendor_bstage WHERE vendor_id = '$send_id'");

$preparedStatement->execute([':vendor_blicense' => $vendor_blicense, ':vendor_btax' => $vendor_btax, ':vendor_tax_check' => $vendor_tax_check, ':vendor_bstage' => $vendor_bstage]);


      move_uploaded_file($temp,$UploadFolder."/".$filer);
      array_push($uploadedFiles, $filer);

    }
  }
}
if($counter>0){
  if(count($errors)>0)
  {
      echo "<b>Errors:</b>";
      echo "<br/><ul>";
      foreach($errors as $error)
      {
          echo "<li>".$error."</li>";
      }
      echo "</ul><br/>";
  }
   
  if(count($uploadedFiles)>0){

echo "successfully uploaded";

}

 
$kone->close();

     }                               
}
else{
  
echo '<script>alert("Please, Select file(s) to upload!")</script>';
echo "<center>Please, Select file(s) to upload!</center>";
}




}


}

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement