douyuefei3546 2016-09-08 09:46
浏览 47
已采纳

只使用多个文件上传器上传一个文件

The following PHP file script uploads only one file although its supposed to send multiple files. Can't understand where it breaks. Tried using foreach but to no avail.

I did a var_dump and they show the correct number of files being sent.

HTML

<form id="fileupload" method="POST" enctype="multipart/form-data">
    <input type="file" name="uploadfile[]" multiple id="uploadfile" />
</form>

PHP

<?php

require_once 'config.php';

############ Edit settings ##############
$UploadDirectory    = './storage/';
#########################################


if(!isset($_FILES["uploadfile"])) {
    die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}

//check if this is an ajax request
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
    die();
}


// Total number of files to be uploaded
$total_files = count($_FILES['uploadfile']['name']);

//check if there is at least 1 file
if($total_files < 1) {
    die();
}


for($i=0; $i<$total_files; $i++) {
    //Is file size is less than allowed size.
    if ($_FILES["uploadfile"]["size"][$i] > 29000000) {
        die("File size is too big!");
    }

    $allowedTypes = array('gif', 'png', 'jpeg', 'jpg', 'pdf', 'xls', 'xlsx', 'doc', 'docx', 'ppt', 'pptx', 'mp3', 'mp4', 'rar', 'zip', 'txt');

    $FileNameFull       = strtolower($_FILES['uploadfile']['name'][$i]);
    $FileNameShort      = pathinfo($FileNameFull, PATHINFO_FILENAME);
    $FileExt            = pathinfo($FileNameFull, PATHINFO_EXTENSION);

    if(!in_array($FileExt, $allowedTypes)) {
        die('Unsupported File format!');
    }

    $FileCode      = rand(0, 9999999999); //Random number to be used to rename actual filename
    $NewFileName        = $FileCode.'.'.$FileExt; //new file name

    if(move_uploaded_file($_FILES['uploadfile']['tmp_name'][$i], $UploadDirectory.$NewFileName )) {
        // Save the file details to database
        //$query = $dbh->prepare("INSERT INTO uploads(file_name, file_code, file_ext, timestamps) VALUES (:file_name, :file_code, :file_ext, :timestamps)");
        /*$query->execute(array(
            ":file_name" => $FileNameShort,
            ":file_code" => $FileCode,
            ":file_ext" => $FileExt,
            ":timestamps" => round(microtime(true) * 1000)
        ));*/

        die('Success! File Uploaded.');
    }
    else{
        die('error uploading File!');
    }

}
  • 写回答

1条回答 默认 最新

  • douyan1882 2016-09-08 09:58
    关注

    Well i will give you a small example:

    HTML:

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

    PHP:

    $totalImage = count($_FILES['uploadfile']['name']);
    
    for($i=0; $i<$totalImage; $i++) {
      $temporaryPathOfImage = $_FILES['uploadfile']['tmp_name'][$i];
    
      if ($temporaryPathOfImage != ""){
        $dirPath = "./storage/" . $_FILES['uploadfile']['name'][$i];
    
        if(move_uploaded_file($temporaryPathOfImage, $dirPath)) {
          //Code Here
        }
      }
    }
    

    Problem:

    die('Success! File Uploaded.');
    

    Remove this line so you will get all images in that loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题