Click here to Skip to main content
16,005,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys, I have a little problem every time that i insert multiple image in database it usually insert blank data instead of the file name of the image.
Here is my code:

PHP
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
  $max_file_size = 999999*999999; //100 kb
  $path = "../images/"; // Upload directory
  $count = 0;
  
  $aid = all_filter($_GET['aid']);
  if(isset($_POST['upload_button']) and $_SERVER['REQUEST_METHOD'] == "POST"){
  
  foreach ($_FILES['files']['name'] as $f => $name) { 
          $files = $_POST['files'.$name];
      if ($_FILES['files']['error'][$f] == 4) {
          continue; 
      }        
      if ($_FILES['files']['error'][$f] == 0) {            
          if ($_FILES['files']['size'][$f] > $max_file_size) {
              $message[] = "$name is too large!.";
              continue; 
          }
      elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
        $message[] = "$name is not a valid format";
        continue; 
      }
          else{ 
              if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) 
              {
                $con->query("insert into gallery (`aid`,`files`) values ('$aid','$files')") or die(mysqli_errno());
                $count++; 
                header("location: view-gallery.php?aid=".$_GET['aid']."&&
                  albumtitle=".$_GET['albumtitle']."&&albumdesc=".$_GET['albumdesc']."
                  &&albumdate=".$_GET['albumdate']." ");
              }
          }
      }
  }
}

<h3>You can upload multiple image in single upload only.</h3>
          <?php
    # error messages
              if (isset($message)) 
              {
                foreach ($message as $msg) {
                  printf("<p class='status'>%s</p></ br>\n", $msg);
                }
              }
              # success message
              if($count !=0){
                printf("<p class='status'>%d files added successfully!</p>\n", $count);
              }
          ?>
          <?php if(isset($_REQUEST['click'])):?>
          <form action="" method="post" enctype="multipart/form-data">
            <input type="file" name="files[]" multiple="multiple" accept="image/*">
            <input type="submit" value="Upload" name="upload_button" class="btn btn-danger">
          </form>
          <?php endif?>
Posted
Updated 26-Feb-15 4:22am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900