简体   繁体   English

在发送给用户之前重命名文件?

[英]Rename file before sending to user?

This is probably a very simple and stupid question to ask, but I have no idea how I would go about doing this.这可能是一个非常简单和愚蠢的问题,但我不知道我会如何 go 这样做。

I am building some file hosting functionality for a client site in PHP.我正在为 PHP 中的客户端站点构建一些文件托管功能。 I have a central repository of files in one directory.我在一个目录中有一个中央文件存储库。 This is because the client wants to maintain one copy of each file (based on the "hash" of the file) if multiple people upload the same file.这是因为如果多人上传同一个文件,客户端希望为每个文件保留一份副本(基于文件的“哈希”)。 Files are renamed before dumping them into this directory (datetime + some code) to avoid filename clashes.文件在转储到此目录之前被重命名(日期时间 + 一些代码)以避免文件名冲突。 The original name(s) of the file are held on the database.文件的原始名称保存在数据库中。

Where the problem is, is that I have no idea how I would go about renaming the file to the original filename when a user requests to download it?问题出在哪里,当用户请求下载文件时,我不知道如何将文件重命名为原始文件名? I could create a temporary copy of it in another directory, but I think this is going to get messy.我可以在另一个目录中创建它的临时副本,但我认为这会变得混乱。 I am hoping there is a better way.我希望有更好的方法。 I have seen another post with我看过另一个帖子

Response.AppendHeader("content-disposition", "attachment; filename='MyFile.jpg'"); 

but that is in ASP.但那是在 ASP 中。 Could anyone point me in the right direction?谁能指出我正确的方向?

Many thanks非常感谢

Ted泰德

in PHP it is similar: see the header function在 PHP 中它是相似的:参见header function

From the example on that page:从该页面上的示例:

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

I don't understand about ASP but maybe this article could make you understand about it.我不了解 ASP,但也许这篇文章可以让您了解它。

Download file with ASP用 ASP 下载文件

and some reference about it ASP Response Object以及一些关于它的参考ASP Response Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM