Abh*_*yal 5 php apache upload chunked large-file-upload
我一直在寻找一个好的方法,并把头撞在墙上。
在一个文件共享服务项目中,我被指派确定上传大文件的最佳方法。
在 stackoverflow 和其他论坛上搜索了很多问题后,我得到了以下结果:
增加脚本最大执行时间以及允许的最大文件大小
这个案子确实不太合适。每次通过普通宽带连接(1mbps-2mbps)上传文件时几乎都会超时。即使上传完成后执行PHP脚本,仍然不能保证上传不会超时。
分块上传。
虽然我有点明白我应该在这里做什么,但我感到困惑的是,假设正在上传一个 1GB 的文件,我正在以 2MB 的块读取它,但如果上传很慢, php 脚本执行将超时并给出错误。
使用 Java 和 Perl 等其他语言?
使用java或perl处理文件上传真的有效吗?
客户端使用的方法不是这里的问题,因为我们将发布一个客户端 SDK,并且可以在其中实现我们选择的方法。客户端和服务器端的实现都将由我们决定。
根据您的观点,考虑到内存使用效率应该很高,并且可能有很多并发上传,哪种方法应该是最好的方法?
Dropbox 和类似的云存储服务如何处理大文件上传并保持快速上传?
我建议您将 PHP I/O 流与 AJAX 结合使用。这将使服务器上的内存占用保持在较低水平,并且您可以轻松构建异步文件上传。请注意,这使用了仅在现代浏览器中可用的 HTML5 API。
将文章中的代码粘贴到此处:
超文本标记语言
<input type="file" name="upload_files" id="upload_files" multiple="multiple">
Run Code Online (Sandbox Code Playgroud)
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);
};
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15546 次 |
| 最近记录: |