Rename file before sending to user?

2019-05-14 07:53发布

问题:

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

回答1:

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


回答2:

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