php 在服务器端处理 plupload 的分块上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9011138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling plupload's chunked uploads on the server-side
提问by TMS
When I use plupload to chunk files (setting option chunk_size
), I get a separate PHP request for each chunk. Looking at $_FILES
variable, each chunk is of type "application/octet-stream"
.
当我使用 plupload 对文件进行分块(设置选项chunk_size
)时,我会收到每个分块的单独 PHP 请求。查看$_FILES
变量,每个块的类型都是"application/octet-stream"
。
Is there any simple, standard and comfortable way how to combine these pieces in PHP at server-side?
是否有任何简单、标准和舒适的方法如何在服务器端在 PHP 中组合这些部分?
With sanity guaranteed (e.g. when one of the pieces is missing etc.).
保证健全(例如,当其中一件丢失时等)。
采纳答案by TMS
In the end I used the code from official example bundled with plupload-1.5.2 (examples/upload.php):
最后,我使用了与 plupload-1.5.2 (examples/upload.php) 捆绑在一起的官方示例中的代码:
http://github.com/moxiecode/plupload/blob/master/examples/upload.php
http://github.com/moxiecode/plupload/blob/master/examples/upload.php
回答by Kris Erickson
Here is the way to parse the chunks, and store the result in $upload_file (change $uploaded_file to match what you need).
这是解析块的方法,并将结果存储在 $upload_file 中(更改 $uploaded_file 以匹配您需要的内容)。
$uploaded_file = '/tmp/uploadFile.jpg';
$chunks = isset($_POST["chunks"]) ? $_POST["chunks"] : 0;
// If we have a chunked operation...
if ($chunks > 0)
{
// Get the chunk number...
$chunk = isset($_POST["chunk"]) ? $_POST["chunk"] : 0;
if ($chunk == 0)
{
if (!isset($_SESSION['last_chunk']))
{
$_SESSION['last_chunk'] = array();
}
$_SESSION['last_chunk'][$_POST['unique_id']] = $chunk;
}
else
{
if ($_SESSION['last_chunk'][$_POST['unique_id']] != $chunk + 1)
{
die('{"jsonrpc" : "2.0", "error" : {"code": 192, "message": "Uploaded chunks out of sequence. Try again."}, "id" : "id"}');
}
}
$tmp_dir = sys_get_temp_dir();
// We need a unique filename to identify the file...
$tmp_filename = $tmp_dir.$_POST['unique_id'];
// If it is the first chunk we have to create the file, othewise we append...
$out_fp = fopen($tmp_filename, $chunk == 0 ? "wb" : "ab");
// The file we are reading from...
$uploaded_file = $_FILES['file']['tmp_name'];
$in_fp = fopen($uploaded_file, "rb");
// Copy the chunk that was uploaded, into the file we are uploading...
while ($buff = fread($in_fp, 4096))
{
fwrite($out_fp, $buff);
}
fclose($out_fp);
fclose($in_fp);
// If we are the last chunk copy the file to the final location and continue on...
if ($chunk == $chunks - 1)
{
copy($tmp_filename, $uploaded_file);
unset($_SESSION['last_chunk'][$_POST['unique_id']]);
if (count($_SESSION['last_chunk']) == 0)
{
unset($_SESSION['last_chunk']);
}
}
else
{
// Otherwise report the result to the uploader...
echo'{"jsonrpc" : "2.0", "result" : null, "id" : "id"}';
}
}