您好,PHP 世界,經過幾個小時的擔憂 :=)
我找到了恢復或暫停上傳的解決方案
在此程式碼片段中,這是伺服器端,而不是任何桌面程式上的用戶端,您必須使用位元組範圍來計算已上傳的位元組和遺失的總位元組。
以下是 PHP 程式碼
<?php
$CHUNK = 8192;
try {
if (!($putData = fopen("php://input", "r")))
throw new Exception("無法取得 PUT 資料。");
$tot_write = 0;
$tmpFileName = "/var/dev/tmp/PUT_FILE";
if (!is_file($tmpFileName)) {
fclose(fopen($tmpFileName, "x")); if (!($fp = fopen($tmpFileName, "w")))
throw new Exception("無法寫入暫存檔");
while ($data = fread($putData, $CHUNK)) {
$chunk_read = strlen($data);
if (($block_write = fwrite($fp, $data)) != $chunk_read)
throw new Exception("無法寫入更多到暫存檔");
$tot_write += $block_write;
}
if (!fclose($fp))
throw new Exception("無法關閉暫存檔");
unset($putData);
} else {
if (!($fp = fopen($tmpFileName, "a")))
throw new Exception("無法寫入暫存檔");
while ($data = fread($putData, $CHUNK)) {
$chunk_read = strlen($data);
if (($block_write = fwrite($fp, $data)) != $chunk_read)
throw new Exception("無法寫入更多到暫存檔");
$tot_write += $block_write;
}
if (!fclose($fp))
throw new Exception("無法關閉暫存檔");
unset($putData);
}
if ($tot_write != $file_size)
throw new Exception("檔案大小錯誤");
$md5_arr = explode(' ', exec("md5sum $tmpFileName"));
$md5 = $md5sum_arr[0];
if ($md5 != $md5sum)
throw new Exception("MD5 錯誤");
} catch (Exception $e) {
echo '', $e->getMessage(), "\n";
}
?>