重命名要即时下载的 pdf 文件

发布于 2024-08-12 14:14:04 字数 266 浏览 3 评论 0原文

给定:服务器上所有上传的pdf文件都带有时间戳前缀。稍后用户可以再次下载这些文件。这些(丑陋的)文件名在服务器上永远不会再改变。

问题:当我提供下载 PDF 文件的选项时,文件名看起来难看且冗长。如何将此名称更改为合理的名称,以便用户下载此文件时,名称看起来不会很奇怪?

由于无法重命名原始文件,我是否需要制作副本?每个可下载的文件不会有额外的开销吗?显然删除复制的文件将是另一个额外的步骤?

文件在客户端完全下载后是否可以重命名文件?

你们有什么建议?

Given: All the uploaded pdf files on server are prefixed with timestamps. Later user can download these files again. These (ugly)filenames would never change again on server.

Question: When I give the option to download PDF file, name of the file looks ugly and lengthy. How can I change this name to something sensible, so that when user downloads this file, name doesn't look weird?

Do I need to make a copy, since renaming the original file is not an option? Wouldn't it be extra overhead for each downloadable file? Obviously deleting the copied file would be another extra step?

Is it possible to rename file once file is completely downloaded on client side?

What do you guys suggest?

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

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

发布评论

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

评论(5

瞎闹 2024-08-19 14:14:04

像这样的东西:

<?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');
?> 

Something like this:

<?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');
?> 
病女 2024-08-19 14:14:04

我需要为最近的一个项目执行此操作,并且对如何实现有点困惑,但在看到 Klaus 的答案后不久就弄清楚了。为了进一步详细说明 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;
}
?>

2) 在面向用户的页面上创建一个指向处理器脚本的链接:

<a href="process.php?file=OriginalFile.pdf&newFile=MyRenamedFile.pdf">DOWNLOAD ME</a>

I needed to do this for a recent project and was a bit confused on how to implement, but figured it out shortly after seeing Klaus's answer. To further elaborate on Klaus' response:

1) Create a file called, say, "process.php". Modify Klaus's code so that it accepts two parameters: the original filename and the new name. My process.php looks like:

<?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;
}
?>

2) Create a link on your user-facing page that points to the processor script:

<a href="process.php?file=OriginalFile.pdf&newFile=MyRenamedFile.pdf">DOWNLOAD ME</a>
橪书 2024-08-19 14:14:04

您需要在 Content-Disposition 标头中设置所需的文件名:

$name = 'desiredname.pdf';
header('Content-Disposition: attachment;filename="' . $name . '"');

但不幸的是,雷蒙德团队开发的某个网络浏览器不会获取此文件名。如果您想让它也能在该浏览器中工作,那么您需要附加与请求路径的最后部分完全相同的文件名。例如:http://example.com/pdf/desiredname.pdf

You need to set the desired filename in the Content-Disposition header:

$name = 'desiredname.pdf';
header('Content-Disposition: attachment;filename="' . $name . '"');

But unfortunately this won't be picked up by a certain webbrowser developed by a team in Redmond. If you want to get it to work in that browser as well, then you need to append the very same filename as last part of the request path. For example: http://example.com/pdf/desiredname.pdf.

献世佛 2024-08-19 14:14:04

所提供内容的文件名在 HTTP 标头中定义。查看标题上的 PHP 页面(以及注释)以获取更多信息。

The filename of the content served is defined in the HTTP headers. Look around the PHP page on headers (also the comments) for more info.

眸中客 2024-08-19 14:14:04

我不久前正在做类似的事情,通过 php 提供文件(在我的例子中,不是重命名它们,而是检查下载它们的人是否已登录并被允许查看这些文件)。我发现 pdf 下载在 IE 中不起作用,除非我还添加了这些标头:

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');

我不知道为什么。进行实验,看看会发生什么。

I was doing something similar a while ago, serving files through php (in my case, not to rename them, but to check that the person downloading them was logged in and was allowed to see those files). I found that pdf downloads didn't work in IE unless I also added these headers:

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');

I'm not sure why. Experiment and see what happens.

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