【问题标题】:Upload folders to server with JavaScript [duplicate]使用 JavaScript 将文件夹上传到服务器 [重复]
【发布时间】:2021-05-02 06:28:42
【问题描述】:

抱歉,我的第一篇文章不符合网站的标准

我重播我的问题

我正在尝试使用 JavaScript 和 php 上传包含 200 多个文件的文件夹。问题是它只是上传了 21 个文件并在显示问题后停止:未定义索引 21。

我的 html 文件:home.html

<form method="POST" id="upload_folder_form">
    <div class="row">
       <input class="action" type="hidden" name="action" value="upload">
       <input class="parent_id" type="hidden" name="parent_id" value="0">
       <div class="form-group">
           <label for="nom">Folder</label>

           <input id="files[]" type="file" name="files[]" class="form-control"  webkitdirectory multiple>

       </div>
       <div class="form-group">
            <div class="col-lg-12">
                 <button type="submit" class="btn color-cmc pull-right" name="btn_envoi">
                      <i class="fa fa-upload"></i> Import
                </button>
                <button type="button" class="btn btn-default pull-right" data-dismiss="modal">Annuler</button>
            </div>
       </div>
    </div>
</form>

我的 Javascripy 文件:Folder.js

$('#upload_folder_form').submit(function (e) {
        e.preventDefault();

        //récuperation des variables
        let form = $('#upload_folder_form');
        let action = $('.action').val();
        let parent_id = $('.parent_id').val();
        let redirect = "http://localhost/archivia/app/controllers/FolderController.php";

        const files = document.querySelector('[type=file]').files;
        let size = files.length;
        const formData = new FormData();

        formData.append('action', action);
        formData.append('parent_id', parent_id);
        formData.append('size', size);

        for (let i = 0; i < files.length; i++) {
            let file = files[i];
            let fileParamName = `file${i}`;
            let filePathParamName = `filepath${i}`;
            formData.append(fileParamName, file);
            formData.append(filePathParamName, file.webkitRelativePath);
        }

        axios.post(redirect, formData).then(function (result) {
            console.log(result.data);
        });

        return false;
    });

我的 PHP 文件:FolderController.php

function upload(){
    $path = "../../public/Archives CMC/";
    $size = $_POST['size'];

    var_dump($_FILES);

    for($i = 0; $i < $size; $i++){
        $folderpath = $path.dirname($_POST['filepath'.$i]);
        $file = $_FILES['file'.$i];
        $filename = $file['name'];
        $file_tmp = $file["tmp_name"];

        if (file_exists($path.$folderpath)){
            move_uploaded_file($file_tmp, $path.$folderpath.'/'.$filename);
        }else{
            mkdir($path.$folderpath,0644,true);
            move_uploaded_file($file_tmp, $path.$folderpath.'/'.$filename);
        }

    }


}

对于超过 30 个文件的文件夹,仅上传 20 个

请问我该怎么办???

谢谢

【问题讨论】:

  • “我很抱歉,因为我的第一篇文章不符合网站的标准” - 下次,请编辑您现有的版本然后提出问题,而不是创建一个新问题。
  • 那么var_dump($_FILES); 显示了什么,实际上print_r($_FILES); 可能更容易阅读
  • 您是否阅读过有关 webkitdirectory 的警告 非标准 此功能是非标准的,不在标准轨道上。不要在面向 Web 的生产站点上使用它:它不适用于每个用户。实现之间也可能存在很大的不兼容性,并且行为可能会在未来发生变化
  • @RiggsFolly 让我给你看图片
  • 也许你应该读一下@CBroe 的答案。似乎是最明显的原因

标签: javascript php ajax


【解决方案1】:

PHP 有一个配置设置,它限制在这样的 POST 请求中接受多少文件上传。

名称为max_file_uploads,默认值为20

https://www.php.net/manual/en/ini.core.php#ini.max-file-uploads

请注意,“可更改模式”在此处显示为 PHP_INI_SYSTEM - 这意味着它只能在 php.ini 或 httpd.conf 中的 set 中设置。因此,如果您无法控制服务器,则必须询问您的服务器管理员/托管服务提供商,他们是否可以为您更改此设置。

这在common pitfalls文件上传工作原理的解释子章节中也有提及,

从 PHP 5.2.12 开始,max_file_uploads 配置设置控制一个请求中可以上传的最大文件数。 如果上传的文件多于限制,则一旦达到限制,$_FILES 将停止处理文件。例如,如果 max_file_uploads 设置为 10,则 $_FILES 将永远不会包含超过 10 个项目。

【讨论】:

    【解决方案2】:

    有许多 PHP 参数在该区域中大致共同作用

    // the max number of file that PHP will accept from upload
    // as it cannot know how many the browser is sending until it gets them
    // php just Stops processing at this number of files
    max_file_uploads = 20
    
    // the max size of any one file that will be accepted
    upload_max_filesize = 2M
    
    // as files are transmitted using POST, this param should also be checked
    // it should be large enough to cope with all the files uploaded
    // So basic calc, assuming all files wont be the max size would be
    
    // post_max_size = max_file_uploads  * upload_max_filesize 
    //
    // However you also have to make room for all the normal html form parameters 
    // that may come as well as the file(s)
    //
    // so 
    // post_max_size = max_file_uploads  * upload_max_filesize + 8M as a guestimate
    post_max_size = 8M    
    
    // of course as all this will occupy memory in PHP while it processes all the
    // files and other field inputs it should be large enough to cope with 
    // all the file and of course some room for the program code
    memory_limit = 128M
    

    忽略这些参数中的数字,它们只是占位符,不是我对您系统的建议,您必须自己解决这些问题

    当您使用 WAMPServer 时,您可以修改 php.ini 文件中的所有这些参数。但是,警告,如果您打算最终将此代码移动到共享主机,您可能无法访问这些参数。因此,在设计具有大量需求的系统时要小心,这可能会迫使您在此代码的最终归宿上花费更多。

    最后,当您在 php.ini 文件中更改这些参数时,请记住退回 Apache 以便获取新值。

    wampmanager->Apache->Service Administration->Restart Service
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-31
      • 2013-10-30
      • 1970-01-01
      • 2016-03-30
      • 2018-06-24
      • 1970-01-01
      • 2022-10-14
      • 2017-04-30
      相关资源
      最近更新 更多