ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

通过php上传大文件(0-5GB)的有效方法

2019-11-21 11:33:18  阅读:480  来源: 互联网

标签:large-file-upload chunked apache upload php


我一直在寻找一种好的方法,然后把头撞在墙上.

在文件共享服务项目中,我被指派确定可用于上传大文件的最佳方法.

在stackoverflow和其他论坛上搜索了很多问题之后,这就是我得到的:

>增加脚本的最大执行时间以及允许的最大文件大小

这种情况确实不合适.通过正常的宽带连接(1mbps-2mbps)上传文件时,几乎会超时.即使在完成上传后执行PHP脚本,仍不能保证上传不会超时.
>分块上传.

尽管我有点理解我应该在这里做什么,但令我感到困惑的是,例如,正在上传一个1GB的文件,而我正在以2MB的块读取它,即使上传速度很慢, PHP脚本执行将超时并给出错误.
>是否使用其他语言,例如Java和Perl?

使用Java或Perl处理文件上传真的有效吗?

客户端使用的方法在这里不是问题,因为我们将发布客户端SDK,并且可以在其中实现我们选择的方法.客户端和服务器端的实现均由我们决定.

您认为哪种方法应该是最好的方法,同时考虑到内存的使用应该是高效的,并且可能会有许多并发上载?

Dropbox和类似的云存储服务如何处理大文件上载,并且仍然保持这种状态?

解决方法:

我建议您将PHP I / O流与AJAX一起使用.这样可以减少服务器上的内存占用,您可以轻松构建异步文件上传.请注意,这使用的HTML5 API仅在现代浏览器中可用.

看看这个帖子:http://www.webiny.com/blog/2012/05/07/webiny-file-upload-with-html5-and-ajax-using-php-streams/

将文章中的代码粘贴到此处:

的HTML

<input type="file" name="upload_files" id="upload_files" multiple="multiple">

JS

function upload(fileInputId, fileIndex)
    {
        // take the file from the input
        var file = document.getElementById(fileInputId).files[fileIndex];
        var reader = new FileReader();
        reader.readAsBinaryString(file); // alternatively you can use readAsDataURL
        reader.onloadend  = function(evt)
        {
                // create XHR instance
                xhr = new XMLHttpRequest();

                // send the file through POST
                xhr.open("POST", 'upload.php', true);

                // make sure we have the sendAsBinary method on all browsers
                XMLHttpRequest.prototype.mySendAsBinary = function(text){
                    var data = new ArrayBuffer(text.length);
                    var ui8a = new Uint8Array(data, 0);
                    for (var i = 0; i < text.length; i++) ui8a[i] = (text.charCodeAt(i) & 0xff);

                    if(typeof window.Blob == "function")
                    {
                         var blob = new Blob([data]);
                    }else{
                         var bb = new (window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)();
                         bb.append(data);
                         var blob = bb.getBlob();
                    }

                    this.send(blob);
                }

                // let's track upload progress
                var eventSource = xhr.upload || xhr;
                eventSource.addEventListener("progress", function(e) {
                    // get percentage of how much of the current file has been sent
                    var position = e.position || e.loaded;
                    var total = e.totalSize || e.total;
                    var percentage = Math.round((position/total)*100);

                    // here you should write your own code how you wish to proces this
                });

                // state change observer - we need to know when and if the file was successfully uploaded
                xhr.onreadystatechange = function()
                {
                    if(xhr.readyState == 4)
                    {
                        if(xhr.status == 200)
                        {
                            // process success
                        }else{
                            // process error
                        }
                    }
                };

                // start sending
                xhr.mySendAsBinary(evt.target.result);
        };
    }

的PHP

// read contents from the input stream
$inputHandler = fopen('php://input', "r");
// create a temp file where to save data from the input stream
$fileHandler = fopen('/tmp/myfile.tmp', "w+");

// save data from the input stream
while(true) {
    $buffer = fgets($inputHandler, 4096);
    if (strlen($buffer) == 0) {
        fclose($inputHandler);
        fclose($fileHandler);
        return true;
    }

    fwrite($fileHandler, $buffer);
}

标签:large-file-upload,chunked,apache,upload,php
来源: https://codeday.me/bug/20191121/2051238.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有