服务器端,获取发送文件的进度

发布于 2024-08-12 09:47:11 字数 124 浏览 5 评论 0原文

基本上,我想做的是在客户端下载文件时检查我的网络服务器已向客户端发送了多少文件。这可能吗? apache 是否提供任何可以帮助我完成任务的模块/扩展?

我使用 Linux 发行版、apache2 和 php5。问候。

Basically, what I want to do is to check how much of a file my webserver has sent to a client, when the client is downloading one. Is this even possible?
Does apache provide any module/extension that would help me accomplish my task?

I use a linux distro, apache2 and php5. Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽袖吟 2024-08-19 09:47:11

如果文件具有正确的“内容长度”标头设置,浏览器将提供此功能。为什么要在页面中实现此功能?

Browser provides this functionality if file has correct "Content-length" header set. Why do you want to implement this in your page?

叫嚣ゝ 2024-08-19 09:47:11

解决了。

我只需使用 PHP 打开要发送给客户端的文件。

$fh = fopen($filePath, 'r');

计算文件大小的 60%

$fileSize = filesize($filePath);
$sizeFirst = floor(($fileSize / 100) * 60);

然后,我通过写入Now $sizeFirst 变量包含文件前 60% 的长度(以数值形式)来 。
为了计算剩余的 40%,我使用:

$sizeLast = $fileSize - $sizeFirst;

现在我可以写出前 60%,执行我的操作,然后写出剩余的 40%。

$dataFirst = fread($fh, $sizeFirst);
echo($dataDirst);

// Do my action here.

$dataSecond = fread($fh, $sizeSecond);
echo($dataSecond);
exit();

我需要设置 header();在写出此内容之前,必须设置 Content-length、Content-type 和 Content-Disposition,以便向客户端发送有效的标头和文件内容。

希望它能帮助某人。

Solved it.

I simply open the file with PHP that I want to send to the client.

$fh = fopen($filePath, 'r');

Then I calculate 60% of the filesize by writing

$fileSize = filesize($filePath);
$sizeFirst = floor(($fileSize / 100) * 60);

Now the $sizeFirst variable contains the length of the first 60% of the file, in a numeric value.
To calculate the rest 40% I use:

$sizeLast = $fileSize - $sizeFirst;

Now I can write out the first 60%, do my action, and then write outh the rest 40%.

$dataFirst = fread($fh, $sizeFirst);
echo($dataDirst);

// Do my action here.

$dataSecond = fread($fh, $sizeSecond);
echo($dataSecond);
exit();

I need to set the header(); before writing out this, the Content-length, Content-type and Content-Disposition must be set in order to send a valid header and filecontent to the client.

Hope it helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文