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。

谢谢你的帮助。

4

1 回答 1

0

嗯,你的代码对我有用:

测试.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
于 2014-03-04T07:12:33.427 回答