通过 readfile() 更改文件的 CRC

发布于 2024-08-14 03:16:34 字数 689 浏览 9 评论 0原文

我设置了这段代码,让用户可以通过我的服务器从他们指定的 URL 下载文件。文件使用 readfile() 进行流式传输,因此它只使用我的带宽。

<?php

set_time_limit(0);

$urlParts = explode("/", $_SERVER['PHP_SELF']);
$file = $urlParts[3];

header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . $file);
header("Content-Transfer-Encoding: binary\n");
readfile($file);

?>

该脚本可以工作,但不会更改下载文件的 CRC 哈希值。我想要它做的是将一些随机位附加到文件末尾,这样它就可以更改哈希值而不破坏它。我尝试在脚本末尾添加类似 echo md5(rand() .time()); 的内容,但它不起作用。

如果这对于像 cURL 这样的东西是可能的,如果有人可以提供一些代码示例,我将不胜感激,因为如果可能的话我会切换到 cURL。

感谢您的帮助。

I have this code set up that lets a user download a file through my server from a URL they specify. The file streams through using readfile() so it only uses my bandwidth.

<?php

set_time_limit(0);

$urlParts = explode("/", $_SERVER['PHP_SELF']);
$file = $urlParts[3];

header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . $file);
header("Content-Transfer-Encoding: binary\n");
readfile($file);

?>

This script works, but it does not change the CRC hash of the downloaded file. What I want it to do is append some random bits to the end of the file so it can change the hash without corrupting it. I have tried adding something like echo md5(rand() . time()); to the end of the script but it doesn't work.

If this is possible with something like cURL I'd appreciate if someone could put up some code samples, because i'd switch to cURL if this was possible.

Thanks for your help.

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

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

发布评论

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

评论(1

燃情 2024-08-21 03:16:34

嗯,你的代码对我有用:

test.php:

set_time_limit(0);

$urlParts = explode("/", $_SERVER['PHP_SELF']);
//$file = $urlParts[3];
$file = 'toread.txt';

header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . $file);
header("Content-Transfer-Encoding: binary\n");
readfile($file);
echo md5(rand() . time());

?>

toread.txt:

这是toread.txt的内容

现在使用curl,我得到以下结果:

>curl -i http://example.com/test.php
HTTP/1.1 200 OK
Date: Tue, 04 Mar 2014 07:09:39 GMT
Server: Apache
Cache-Control: public, must-revalidate
Pragma: hack
Content-Disposition: attachment; filename=toread.txt
Content-Transfer-Encoding: binary
Transfer-Encoding: chunked
Content-Type: application/force-download
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0

This is the content of toread.txt38d8a8009fad7315bdf5e823a06018e7

第二个:

>curl -i http://example.com/test.php 
HTTP/1.1 200 OK
Date: Tue, 04 Mar 2014 07:09:57 GMT
Server: Apache
Cache-Control: public, must-revalidate
Pragma: hack
Content-Disposition: attachment; filename=toread.txt
Content-Transfer-Encoding: binary
Transfer-Encoding: chunked
Content-Type: application/force-download
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0

This is the content of toread.txt3b87356ea9ee007b70cfd619e31da950

Hmm, your code works for me:

test.php:

set_time_limit(0);

$urlParts = explode("/", $_SERVER['PHP_SELF']);
//$file = $urlParts[3];
$file = 'toread.txt';

header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . $file);
header("Content-Transfer-Encoding: binary\n");
readfile($file);
echo md5(rand() . time());

?>

toread.txt:

This is the content of toread.txt

Now using curl, I get the following results:

>curl -i http://example.com/test.php
HTTP/1.1 200 OK
Date: Tue, 04 Mar 2014 07:09:39 GMT
Server: Apache
Cache-Control: public, must-revalidate
Pragma: hack
Content-Disposition: attachment; filename=toread.txt
Content-Transfer-Encoding: binary
Transfer-Encoding: chunked
Content-Type: application/force-download
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0

This is the content of toread.txt38d8a8009fad7315bdf5e823a06018e7

And the second one:

>curl -i http://example.com/test.php 
HTTP/1.1 200 OK
Date: Tue, 04 Mar 2014 07:09:57 GMT
Server: Apache
Cache-Control: public, must-revalidate
Pragma: hack
Content-Disposition: attachment; filename=toread.txt
Content-Transfer-Encoding: binary
Transfer-Encoding: chunked
Content-Type: application/force-download
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0

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