多次上传PHP图片,仅上传所选的第一张图片

时间:2019-05-08 10:45:52

标签: php file upload

我已经设置了多个文件上传表单。但是,当我选择多张图像并单击“上传”时,仅上传第一张图像,而不上传图像阵列吗?干杯。

<?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"];
    }
} ?>

2 个答案:

答案 0 :(得分:5)

这是您需要做的:

  • 必须将输入名称定义为数组,即name =“ inputName []”
  • 输入元素必须具有多个=“ multiple”或仅多个
  • 在您的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

        }
    }
}

希望这对您有帮助!

答案 1 :(得分:1)

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

<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"];
}

}?>