當我使用plupload加載塊文件(設置選項chunk_size
)時,我爲每個塊獲得單獨的PHP請求。查看$_FILES
變量,每個塊的類型爲"application/octet-stream"
。在服務器端處理plupload的分塊上載
是否有任何簡單,標準和舒適的方式如何在服務器端PHP中組合這些片段?
保證有理智(例如,當其中一件丟失等)。
當我使用plupload加載塊文件(設置選項chunk_size
)時,我爲每個塊獲得單獨的PHP請求。查看$_FILES
變量,每個塊的類型爲"application/octet-stream"
。在服務器端處理plupload的分塊上載
是否有任何簡單,標準和舒適的方式如何在服務器端PHP中組合這些片段?
保證有理智(例如,當其中一件丟失等)。
我最終使用從與plupload-1.5.2(實施例/ upload.php的)捆綁官方例子的代碼:
http://github.com/moxiecode/plupload/blob/master/examples/upload.php
以下是解析塊的方法,並將結果存儲在$ 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"}';
}
}
由於克里斯。這是你通常用於這個目的的代碼嗎?隨着plupload或其他庫? – TMS
我將這個與plupload flash組件一起使用。它應該與所有使用塊的plupload兼容。 –
Kris,但是我沒有$ _POST請求中的'unique_id'。你能否告訴我你是如何設置plupload對象的?你使用'multipart:true'嗎? – TMS