问题:
如何在服务器和客户端不阻塞的情况下实时读取和回显正在写入服务器的上传文件的文件大小?
上下文:
从 fetch()
发出的 POST
请求写入服务器的文件上传进度,其中 body
设置为 Blob
、File
、TypedArray
或 ArrayBuffer
对象。
当前的实现在 body
对象处设置 File
对象传递给 fetch()
的第二个参数。
要求:
读取并回显
以text/event-stream
形式向客户端写入正在服务器文件系统中的文件的文件大小。当所有字节都被写入时停止,这些字节作为变量提供给脚本作为 GET
请求中的查询字符串参数。当前文件的读取发生在一个单独的脚本环境中,其中对应该读取文件的脚本的 GET
调用是在对将文件写入服务器的脚本的 POST
之后进行的。
尚未达到对将文件写入服务器或读取文件以获得当前文件大小的潜在问题的错误处理,尽管一旦文件大小部分的 echo
完成,这将是下一步。
目前正在尝试使用 php
来满足要求。虽然也对 c
、bash
、nodejs
、python
感兴趣;或可用于执行相同任务的其他语言或方法。
客户端 javascript
部分不是问题。只是不太精通 php
(万维网上使用的最常见的服务器端语言之一),无法在不包含不必要部分的情况下实现该模式。
动机:
Progress indicators for fetch?
相关:
问题:
获取
PHP Notice: Undefined index: HTTP_LAST_EVENT_ID in stream.php on line 7
在终端
。
另外,如果替换
while(file_exists($_GET["filename"])
&& filesize($_GET["filename"]) < intval($_GET["filesize"]))
对于
while(true)
在 EventSource
处产生错误。
没有 sleep()
调用,正确的文件大小被分派(dispatch)给 3.3MB
文件的 message
事件,3321824
, 分别在console
61921
, 26214
, 和 38093
打印同一个文件三个次。预期结果是文件的文件大小,因为文件正在写入
stream_copy_to_stream($input, $file);
而不是上传文件对象的文件大小。 fopen()
或 stream_copy_to_stream()
是否阻塞了 stream.php
中的其他不同的 php
进程?
到目前为止尝试过:
php
归于
- Beyond $_POST, $_GET and $_FILE: Working with Blob in JavaScriptPHP
- Introduction to Server-Sent Events with PHP example
php
// can we merge `data.php`, `stream.php` to same file?
// can we use `STREAM_NOTIFY_PROGRESS`
// "Indicates current progress of the stream transfer
// in bytes_transferred and possibly bytes_max as well" to read bytes?
// do we need to call `stream_set_blocking` to `false`
// data.php
<?php
$filename = $_SERVER["HTTP_X_FILENAME"];
$input = fopen("php://input", "rb");
$file = fopen($filename, "wb");
stream_copy_to_stream($input, $file);
fclose($input);
fclose($file);
echo "upload of " . $filename . " successful";
?>
// stream.php
<?php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection: keep-alive");
// `PHP Notice: Undefined index: HTTP_LAST_EVENT_ID in stream.php on line 7` ?
$lastId = $_SERVER["HTTP_LAST_EVENT_ID"] || 0;
if (isset($lastId) && !empty($lastId) && is_numeric($lastId)) {
$lastId = intval($lastId);
$lastId++;
}
// else {
// $lastId = 0;
// }
// while current file size read is less than or equal to
// `$_GET["filesize"]` of `$_GET["filename"]`
// how to loop only when above is `true`
while (true) {
$upload = $_GET["filename"];
// is this the correct function and variable to use
// to get written bytes of `stream_copy_to_stream($input, $file);`?
$data = filesize($upload);
// $data = $_GET["filename"] . " " . $_GET["filesize"];
if ($data) {
sendMessage($lastId, $data);
$lastId++;
}
// else {
// close stream
// }
// not necessary here, though without thousands of `message` events
// will be dispatched
// sleep(1);
}
function sendMessage($id, $data) {
echo "id: $id\n";
echo "data: $data\n\n";
ob_flush();
flush();
}
?>
javascript
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="file">
<progress value="0" max="0" step="1"></progress>
<script>
const [url, stream, header] = ["data.php", "stream.php", "x-filename"];
const [input, progress, handleFile] = [
document.querySelector("input[type=file]")
, document.querySelector("progress")
, (event) => {
const [file] = input.files;
const [{size:filesize, name:filename}, headers, params] = [
file, new Headers(), new URLSearchParams()
];
// set `filename`, `filesize` as search parameters for `stream` URL
Object.entries({filename, filesize})
.forEach(([...props]) => params.append.apply(params, props));
// set header for `POST`
headers.append(header, filename);
// reset `progress.value` set `progress.max` to `filesize`
[progress.value, progress.max] = [0, filesize];
const [request, source] = [
new Request(url, {
method:"POST", headers:headers, body:file
})
// https://stackoverflow.com/a/42330433/
, new EventSource(`${stream}?${params.toString()}`)
];
source.addEventListener("message", (e) => {
// update `progress` here,
// call `.close()` when `e.data === filesize`
// `progress.value = e.data`, should be this simple
console.log(e.data, e.lastEventId);
}, true);
source.addEventListener("open", (e) => {
console.log("fetch upload progress open");
}, true);
source.addEventListener("error", (e) => {
console.error("fetch upload progress error");
}, true);
// sanity check for tests,
// we don't need `source` when `e.data === filesize`;
// we could call `.close()` within `message` event handler
setTimeout(() => source.close(), 30000);
// we don't need `source' to be in `Promise` chain,
// though we could resolve if `e.data === filesize`
// before `response`, then wait for `.text()`; etc.
// TODO: if and where to merge or branch `EventSource`,
// `fetch` to single or two `Promise` chains
const upload = fetch(request);
upload
.then(response => response.text())
.then(res => console.log(res))
.catch(err => console.error(err));
}
];
input.addEventListener("change", handleFile, true);
</script>
</body>
</html>
最佳答案
您需要clearstatcache获得真实的文件大小。修复了一些其他位后,您的 stream.php 可能如下所示:
<?php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection: keep-alive");
// Check if the header's been sent to avoid `PHP Notice: Undefined index: HTTP_LAST_EVENT_ID in stream.php on line `
// php 7+
//$lastId = $_SERVER["HTTP_LAST_EVENT_ID"] ?? 0;
// php < 7
$lastId = isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? intval($_SERVER["HTTP_LAST_EVENT_ID"]) : 0;
$upload = $_GET["filename"];
$data = 0;
// if file already exists, its initial size can be bigger than the new one, so we need to ignore it
$wasLess = $lastId != 0;
while ($data < $_GET["filesize"] || !$wasLess) {
// system calls are expensive and are being cached with assumption that in most cases file stats do not change often
// so we clear cache to get most up to date data
clearstatcache(true, $upload);
$data = filesize($upload);
$wasLess |= $data < $_GET["filesize"];
// don't send stale filesize
if ($wasLess) {
sendMessage($lastId, $data);
$lastId++;
}
// not necessary here, though without thousands of `message` events will be dispatched
//sleep(1);
// millions on poor connection and large files. 1 second might be too much, but 50 messages a second must be okay
usleep(20000);
}
function sendMessage($id, $data)
{
echo "id: $id\n";
echo "data: $data\n\n";
ob_flush();
// no need to flush(). It adds content length of the chunk to the stream
// flush();
}
一些注意事项:
安全。我的意思是运气。据我所知,这是一个概念证明,安全是最不关心的问题,但免责声明应该在那里。这种方法存在根本性缺陷,只有在您不关心 DOS 攻击或文件信息泄露时才应使用。
中央处理器。如果没有 usleep
,脚本将消耗 100% 的单个内核。如果长时间休眠,您将面临在一次迭代中上传整个文件的风险,并且永远不会满足退出条件。如果你在本地测试它,usleep
应该完全删除,因为在本地上传 MB 是毫秒级的事情。
打开连接。 apache 和 nginx/fpm 都有有限数量的 php 进程可以满足请求。单个文件上传将花费 2 为上传文件所需的时间。如果带宽较慢或伪造请求,这段时间可能会很长,并且 Web 服务器可能会开始拒绝请求。
客户端部分。您需要分析响应并最终在文件完全上传时停止监听事件。
编辑:
为了使它或多或少对生产友好,您将需要一个内存存储,如 Redis 或 Memcache 来存储文件元数据。
发出一个 post 请求,添加一个唯一的 token 来标识文件和文件大小。
在你的 javascript 中:
const fileId = Math.random().toString(36).substr(2); // or anything more unique
...
const [request, source] = [
new Request(`${url}?fileId=${fileId}&size=${filesize}`, {
method:"POST", headers:headers, body:file
})
, new EventSource(`${stream}?fileId=${fileId}`)
];
....
在data.php中注册token并分块上报进度:
....
$fileId = $_GET['fileId'];
$fileSize = $_GET['size'];
setUnique($fileId, 0, $fileSize);
while ($uploaded = stream_copy_to_stream($input, $file, 1024)) {
updateProgress($id, $uploaded);
}
....
/**
* Check if Id is unique, and store processed as 0, and full_size as $size
* Set reasonable TTL for the key, e.g. 1hr
*
* @param string $id
* @param int $size
* @throws Exception if id is not unique
*/
function setUnique($id, $size) {
// implement with your storage of choice
}
/**
* Updates uploaded size for the given file
*
* @param string $id
* @param int $processed
*/
function updateProgress($id, $processed) {
// implement with your storage of choice
}
所以你的 stream.php 根本不需要打盘,只要 UX 可以接受就可以休眠:
....
list($progress, $size) = getProgress('non_existing_key_to_init_default_values');
$lastId = 0;
while ($progress < $size) {
list($progress, $size) = getProgress($_GET["fileId"]);
sendMessage($lastId, $progress);
$lastId++;
sleep(1);
}
.....
/**
* Get progress of the file upload.
* If id is not there yet, returns [0, PHP_INT_MAX]
*
* @param $id
* @return array $bytesUploaded, $fileSize
*/
function getProgress($id) {
// implement with your storage of choice
}
2个open connections的问题无法解决,除非你放弃EventSource换回old good pulling。没有循环的 stream.php 的响应时间是毫秒级的事情,一直保持连接打开是相当浪费的,除非你需要每秒更新数百次。
关于javascript - 如何在不阻塞服务器和客户端的情况下实时读取和回显在服务器上写入的上传文件的文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42475492/