簡體   English   中英

多次上傳PHP圖片,僅上傳所選的第一張圖片

[英]Multiple PHP image upload, only uploads first image selected

我已經設置了多個文件上傳表單。 但是,當我選擇多張圖像並單擊“上傳”時,僅上傳第一張圖像,而不上傳圖像陣列嗎? 干杯。

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

這是您需要做的:

  • 輸入名稱必須定義為數組,即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

        }
    }
}

希望這會有所幫助!

我已經對您的代碼進行了一些更改以使用它。可能對您有所幫助,請檢查

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

}?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM