Rename file before sending to user?

2019-05-14 07:47发布

This is probably a very simple and stupid question to ask, but I have no idea how I would go about doing this.

I am building some file hosting functionality for a client site in 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. Could anyone point me in the right direction?

Many thanks

Ted

2条回答
劫难
2楼-- · 2019-05-14 08:22

I don't understand about ASP but maybe this article could make you understand about it.

Download file with ASP

and some reference about it ASP Response Object

查看更多
走好不送
3楼-- · 2019-05-14 08:34

in PHP it is similar: see the 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');
?>   
查看更多
登录 后发表回答