【问题标题】:How to upload/store multiple images in mysql database in php如何在php中的mysql数据库中上传/存储多个图像
【发布时间】:2022-01-29 18:00:30
【问题描述】:

这里我正在制作一个表单,用户可以在其中上传多张图片

HTML 代码如下

<input type='file' name='attachPhoto1' multiple />

PHP 代码如下

        $tmp_file = $_FILES['attachPhoto1']['tmp_name'];
        $target_file = basename($_FILES['attachPhoto1']['name']);
        $upload_dir = "userUploads";
        move_uploaded_file($tmp_file,$upload_dir."/".$target_file);

但是当我上传多张图片时,只有一张图片会上传到userUploads 文件夹中。所以请帮我解决这个问题。

【问题讨论】:

标签: php html mysql


【解决方案1】:

使用<input type='file' name='attachPhoto1[]' multiple />。 注意方括号(突出显示它们对我不起作用)。 在 PHP 中,您应该遍历 $attachPhoto1[] 数组的成员。

【讨论】:

  • 提供更多细节?
【解决方案2】:

当您上传和发布多个图像文件时,您的输入应该是这样的

<input type='file' name='attachPhoto1[]' multiple />

你的表单应该是这样的

<form action="" method="post" name="" id="" enctype="multipart/form-data">

以下是我使用的非常多的工作代码,您可以根据需要进行调整;

if(!empty($_FILES['attachPhoto1']['name'])) {
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $error_uploads = 0;
        $total_uploads = array();
        $upload_path = 'upload/';
        foreach($_FILES['attachPhoto1']['name'] as $key => $value) {
            $temp = explode(".", $_FILES['attachPhoto1']['name'][$key]);
            $extension = end($temp);
            if ($_FILES["files"]["type"][$key] != "image/gif"
                && $_FILES["files"]["type"][$key] != "image/jpeg"
                && $_FILES["files"]["type"][$key] != "image/jpg"
                && $_FILES["files"]["type"][$key] != "image/pjpeg"
                && $_FILES["files"]["type"][$key] != "image/x-png"
                && $_FILES["files"]["type"][$key] != "image/png"
                && !in_array($extension, $allowedExts)) {
                $error_uploads++;
                continue;
            }
            $file_name = time().rand(1,5).rand(6,10).'_'.str_replace(' ', '_', $_FILES["attachPhoto1"]['name'][$key]);
            if(move_uploaded_file($_FILES["attachPhoto1"]['tmp_name'][$key], $upload_path.$file_name)) {
                $total_uploads[] = $file_name;
            } else {
                $error_uploads++;
            }
        }
        if(sizeof($total_uploads)) {
        //Do what ever you like after file uploads, you can run query here to save it in database or set redirection after success upload
        }
        }
    }

【讨论】:

  • 是的,我的表格就像你问的那样,但它不起作用
  • 回答编辑和工作的php代码,根据您的要求调整代码
【解决方案3】:

参考此代码在指定文件夹中上传多个图像

<?php
if (isset($_POST['submit'])) {
   $j = 0;                       // Variable for indexing uploaded image.
   $target_path = "uploads/";    // Declaring Path for uploaded images.

   // --- Loop to get individual element from the array
   for ($i = 0; $i < count($_FILES['file']['name']); $i++) {   
      $validextensions = array("jpeg", "jpg", "png");    // Extensions which are allowed.
      $ext = explode('.', basename($_FILES['file']['name'][$i])); // Explode file name from dot(.)
      $file_extension = end($ext); // Store extensions in the variable.
      $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];     // Set the target path with a new name of image.
      $j = $j + 1;     // Increment the number of uploaded images according to the files in array.

      // --- Approx. 100kb files can be uploaded.
      if (($_FILES["file"]["size"][$i] < 100000) && in_array($file_extension, $validextensions)) {
         if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
      // If file moved to uploads folder.
            echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
         } else {       //  If File Was Not Moved.
            echo $j. ').<span id="error">please try again!.</span><br/><br/>';
         }
      } else {          //   If File Size And File Type Was Incorrect.
         echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
      }
   }
}
?>

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2015-01-01
    • 1970-01-01
    • 2012-06-08
    相关资源
    最近更新 更多