在文件下载之前用PHP重命名文件,而不将其保存到服务器


Rename a file with PHP right before it downloads without saving it to the server?

我有一个Adobe Illustrator文件(AI),我们目前在网站上有一个链接,然后将该文件下载到您的计算机上。

链接看起来像这样
http://domain.com/crm/index.php?entryPoint=fileupload_download&id=22440435-e8ee-bd6f-7612-533b2cd7690f&field=fuaifile_c&type=D1_Designs

我需要做的是在下载时重命名此文件。

因此,我想知道是否有可能在下载之前通过另一个PHP文件进行下载,这样我就可以随时更改用户下载的文件名。我无法更改服务器上的文件名,但当它下载时,如果可能的话,我希望能够在文件名中添加一些ID号?有什么想法可以在不需要用新名称在服务器上重新保存图像的情况下实现这一点吗?

您要查找的是RFC2183:中指定的Content-Disposition标头

Content-Disposition: attachment; filename=example.ai

您可以使用PHP header()函数设置此标头。

这很难看,并假设这些文件不是超出内存限制的"大"文件,而是

$data = file_get_contents($original_url);
header('Content-disposition: attachment; filename="new name with id numbers');
header("Content-type: application/octet-stream");
echo $data;

你可以随时增强这一功能来进行字节服务-从原始url中抽取10k,向用户吐出10k,等等…

只需设置Content-Disposition:

header('Content-Disposition: attachment; filename="downloaded.pdf"');

(示例取自PHP文档:http://www.php.net/manual/en/function.header.php)。

添加id:

$id = generateIdFromSomewhere();
header('Content-Disposition: attachment; filename="downloaded'.$id.'.pdf"');