【问题标题】:Multiple PHP image upload, only uploads first image selected多张PHP图片上传,只上传选择的第一张图片
【发布时间】:2019-05-08 10:45:51
【问题描述】:

我已经设置了一个多文件上传表单。但是,当我选择多张图片并单击上传时,只上传第一张图片而不上传图片数组?干杯。

<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
    // Check if file was uploaded without errors
    if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        $filename = $_FILES["photo"]["name"];
        $filetype = $_FILES["photo"]["type"];
        $filesize = $_FILES["photo"]["size"];

        // Verify file extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");

        // Verify file size - 20MB maximum
        $maxsize = 20 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");

        // Verify MYME type of the file
        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("upload/" . $filename)){
                echo $filename . " is already exists.";
            } else{
                move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $filename);
                echo "Your file was uploaded successfully.";
            } 
        } else{
            echo "Error: There was a problem uploading your file. Please try again."; 
        }
    } else{
        echo "Error: " . $_FILES["photo"]["error"];
    }
} ?>

【问题讨论】:

  • 因为您的代码不会遍历 files 数组
  • 好的..我该怎么做...
  • 您必须在 for 循环中进行上传..这个不会起作用..文件名也应该作为数组给出

标签: php file upload


【解决方案1】:

这是你需要做的:

  • 输入名称必须定义为数组,即 name="inputName[]"
  • 输入元素必须有多个=“多个”或只有多个
  • 在您的 PHP 文件中使用语法“$_FILES['inputName']['param'][index]”
  • 确保查找空文件名和路径,该数组可能包含空字符串。在计数之前使用 array_filter()。
  • 这是一个糟糕的示例(仅显示相关代码)

HTML:

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

PHP:

//$files = array_filter($_FILES['upload']['name']); //something like that to be used before processing files.

// Count # of uploaded files in array
$total = count($_FILES['upload']['name']);

// Loop through each file
for( $i=0 ; $i < $total ; $i++ ) {
    //Get the temp file path
    $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

    //Make sure we have a file path
    if ($tmpFilePath != ""){
        //Setup our new file path
        $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

        //Upload the file into the temp dir
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {

          //Handle other code here

        }
    }
}

希望对您有所帮助!

【讨论】:

    【解决方案2】:

    我已经对您的代码进行了一些更改,请使用它。可能会对您有所帮助,也请检查

    <input type="file" multiple>
    
    
    
      if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        $countfiles = count($_FILES['photo']['name']);
        for($i=0;$i<$countfiles;$i++){
            $filename = $_FILES["photo"]["name"][$i];
            $filetype = $_FILES["photo"]["type"][$i];
            $filesize = $_FILES["photo"]["size"][$i];
    
            // Verify file extension
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format.");
    
           // Verify file size - 20MB maximum
           $maxsize = 20 * 1024 * 1024;
           if($filesize > $maxsize) die("Error: File size is larger than the allowed limit.");
    
           // Verify MYME type of the file
           if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("upload/" . $filename)){
                echo $filename . " is already exists.";
            } else{
                move_uploaded_file($_FILES["photo"]["tmp_name"][$i], "upload/" . $filename);
                echo "Your file was uploaded successfully.";
            } 
        } else{
            echo "Error: There was a problem uploading your file. Please try again."; 
        }
      }
    } else{
        echo "Error: " . $_FILES["photo"]["error"];
    }
    

    } ?>

    【讨论】:

    • OP 为什么要使用它好的答案将始终解释所做的工作以及这样做的原因,不仅适用于 OP,而且适用于 SO 的未来访问者,他们可能会发现这个问题并正在阅读您的答案。如果回答得很好,也许他们会支持它。
    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多