如何在mysqli中使用php在1个上传中插入多个图像 [英] How can insert multiple image in 1 upload using php in mysqli

查看:81
本文介绍了如何在mysqli中使用php在1个上传中插入多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,我每次在数据库中插入多个图像时都会遇到一些问题,它通常会插入空白数据而不是图像的文件名。

这是我的代码:



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:

$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?>

推荐答案

valid_formats = array( jpg png gif zip BMP);
valid_formats = array("jpg", "png", "gif", "zip", "bmp");


max_file_size = 999999 * 999999; // 100 kb
max_file_size = 999999*999999; //100 kb


path = ../ images /; // 上传目录
path = "../images/"; // Upload directory


这篇关于如何在mysqli中使用php在1个上传中插入多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆