给定:服务器上所有上传的pdf文件都带有时间戳前缀.以后用户可以再次下载这些文件.这些(丑陋)文件名永远不会在服务器上再次更改.
问题:当我提供下载PDF文件的选项时,该文件的名称看起来很丑陋和冗长.如何将此名称更改为合理的名称,以便在用户下载此文件时,名称看起来并不奇怪?
我是否需要制作副本,因为重命名原始文件不是一种选择?这不是每个可下载文件的额外开销吗?显然删除复制的文件将是另一个额外的步骤?
一旦文件在客户端完全下载,是否可以重命名文件?
你们有什么建议?
像这样的东西:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
Run Code Online (Sandbox Code Playgroud)
小智 5
我需要为最近的一个项目做这件事,并且对如何实施有点困惑,但在看到Klaus的回答后不久便想出来了.进一步阐述克劳斯的回应:
1)创建一个名为"process.php"的文件.修改Klaus的代码,使其接受两个参数:原始文件名和新名称.我的process.php看起来像:
<?php
$file = $_GET['file'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.$_GET['newFile']);
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
Run Code Online (Sandbox Code Playgroud)
2)在面向用户的页面上创建指向处理器脚本的链接:
<a href="process.php?file=OriginalFile.pdf&newFile=MyRenamedFile.pdf">DOWNLOAD ME</a>
Run Code Online (Sandbox Code Playgroud)