【问题标题】:Not able to upload multiple files in php无法在php中上传多个文件
【发布时间】:2016-11-30 06:57:12
【问题描述】:

我在 PHP 中创建了一个多文件上传功能。 当我尝试上传文件时,我在处理请求时一次选择 15 个文件,只有前四个文件存储在 db 中,并且每次都将所有 15 个文件上传到目录中。

我还检查了 PHP.ini 文件,最大文件上传限制为 50 个 pre request,post 最大大小为 5M。仍然找不到任何解决问题的方法。

为什么只保存前四个文件的记录。

if($_POST['submit']== 'Upload_picture')
    {
        //echo "<pre>";     print_r($_FILES['upload_picture']['name']);
        //die;

        $album_title = $_GET['name'];
        $album_id = $_GET['album'];
        $album_dir = "../images/album/$album_title/"; #album path root directory
        $db_album_dir = "images/album/$album_title/"; #batadase album path root directory
        $error = array();
        $extension = array('jpg','gif','png','jpeg');
        foreach($_FILES['upload_picture']['tmp_name'] as $key => $tmp_name){
            $file_name = $_FILES['upload_picture']['name'][$key];
            $file_tmp = $_FILES['upload_picture']['tmp_name'][$key];
            $ext = pathinfo($file_name, PATHINFO_EXTENSION);
            $new_filename = rand().".".$ext; #changing name
            if(in_array($ext,$extension))
            {
                if(move_uploaded_file($file_tmp= $_FILES['upload_picture']['tmp_name'][$key],"$album_dir$new_filename"))
                {
                # insert record database

                    $values = [
                    'album_id' =>$album_id,
                    'image_name' => $new_filename,
                    'album_name' => $album_title,
                    'image_path' => $db_album_dir.$new_filename,
                    'uploaded_date' => date("Y/m/d h:i:s a")
                    ];
                    include_once "action_page.php";
                    $tablename = 'album_picture';
                    $abc = new Demo();
                    $res = $abc->insert($tablename,$values);
                    unset($abc);
                    $_SESSION['upload_success'] = "Files Uploaded succesfully"; 
                    header("location:../upload_album.php?album=$album_id&name=$album_title");

                }
                else
                {
                        $_SESSION['upload_error'] = "Something went wrong, files cannot be uploaded"; 
                }

            }
            else
            {
                $_SESSION['upload_warning'] = "Please upload file"; 
                header("location:../upload_album.php?album=$album_id&name=$album_title");
            }
        }//EOF FROEACH

【问题讨论】:

  • 首先通过在 foreach() 循环中打印名称来检查所有文件是否已成功发布
  • 是的,所有文件都是根据请求发布的

标签: php


【解决方案1】:

您的文件输入字段应该是这样的:

 <input type="file" id="file" name="name[]" multiple />

if(isset($_FILES['upload_picture']['name']))
    {
        $file_name_all="";
        for($i=0; $i<count($_FILES['upload_picture']['name']); $i++) 
        {
               $tmpFilePath = $_FILES['upload_picture']['tmp_name'][$i];    
               if ($tmpFilePath != "")
               {    
                   $path = "propertyimages/"; // create folder 
                   $name = $_FILES['upload_picture']['name'][$i];
                  $size = $_FILES['upload_picture']['size'][$i];

                   list($txt, $ext) = explode(".", $name);
                   $file= time().substr(str_replace(" ", "_", $txt), 0);
                   $info = pathinfo($file);
                   $filename = $file.".".$ext;
                   if(move_uploaded_file($_FILES['upload_picture']['tmp_name'][$i], $path.$filename)) 
                   { 
                      $file_name_all.=$filename."*";
                   }
             }
        }
         $filepath = rtrim($file_name_all, '*'); 
         $tablename = 'album_picture';
         $abc = new Demo();
         $res = $abc->insert($tablename,$values);
         unset($abc);
        }
        else
    {
        $filepath="";
    }

【讨论】:

    猜你喜欢
    • 2023-01-25
    • 1970-01-01
    • 2012-09-18
    • 2016-02-17
    • 1970-01-01
    • 2018-01-26
    • 2021-08-23
    • 2018-11-20
    相关资源
    最近更新 更多