简体   繁体   English

将多张图片上传到服务器

[英]Uploading multi images to server

I am working on the following code. 我正在研究以下代码。 Why am I not able to upload all images stored in the files? 为什么我无法上传文件中存储的所有图像? The code only upload one image into the server. 该代码仅将one图像上传到服务器。

HTML 的HTML

        <form id="uploader" action="#" method="post">
            Package Name
            <input type="text" name="rental_id_box" id="rental_id_box"/>
            <br /> Package Images
            <input id="selected_imgs" name="selected_imgs" type="file" multiple>
        </form>

JS: JS:

    $("#btn-upload").on("click", function() {

      var fd = new FormData($('form')[0]);
    //fd.append( 'file', input.files[0] );
      var request = $.ajax({
        type: "POST",
        url: "upload.php",
        data: fd,
        processData: false,
        contentType: false,
        cache: false,
        beforeSend: function() {
        console.log(fd);
        }
      });

      request.done(function(data) {
          $("#view").html(data)
      });

      request.fail(function(jqXHR, textStatus) {
        console.log("Request failed: " + textStatus);
      });
    });

and PHP 和PHP

<?php
$title = $_POST['rental_id_box'];
foreach($_FILES as $index => $file)
    {
        $fileName     = $file['name'];
        $fileTempName = $file['tmp_name'];

        if(!empty($file['error'][$index]))
        {
            return false;
        }
        if(!empty($fileTempName) && is_uploaded_file($fileTempName))
        {
            move_uploaded_file($fileTempName, "uploads/". $fileName);
            echo '<p>Click <strong><a href="uploads/' . $fileName . '" target="_blank">' . $fileName . '</a></strong> to download it.</p>';
        }
    }
...&& is_uploaded_file($_FILES['name']['tmp_name'])
if(count($_FILES['selected_imgs']['name']) > 0){
for($i=0; $i<count($_FILES['selected_imgs']['name']); $i++) {
    $tmpFilePath = $_FILES['selected_imgs']['tmp_name'][$i];
    print_r($tmpFilePath);
    if(($tmpFilePath != "")&&(is_uploaded_file($tmpFilePath))){            
        $filePath = "upload/" . date('d-m-Y-H-i-s').'-'.$_FILES['selected_imgs']['name'][0];
        move_uploaded_file($tmpFilePath, $filePath);
      }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM