Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a file with a different name to the stored name

Is it possible to let your user download a file with a different name?

For example, there is a file called "4324ffsd34.jpg". I want people to download it via download.php, with a different name (like "filetodownload.jpg"), without renaming the original file.

like image 865
Utku Dalmaz Avatar asked Oct 27 '09 01:10

Utku Dalmaz


People also ask

How can I change file name while downloading?

Now, the only way to change the name of the file/document is to save the file/document with what ever name it came with in the initial download and then go back to the location where I saved the file/document, click on the file/folder, click on rename and rename the file/document to the name I wanted at the initial ...

How do I specify a fileName in wget?

By default, downloaded file will be saved with the last name mentioned in the URL. To save file with a different name option O can be used. Syntax: wget -O <fileName><URL>


2 Answers

Sure, use a Content-disposition header

header('Content-Disposition: attachment; filename="filetodownload.jpg"'); 

if you wish to provide a default filename, but not automatic download, this seems to work.

header('Content-Disposition: filename="filetodownload.jpg"'); 
like image 152
David Snabel-Caunt Avatar answered Sep 23 '22 01:09

David Snabel-Caunt


Sure you can, just try something like this:

$original_filename = '4324ffsd34.jpg'; $new_filename = 'my_new_filename_is_detailled.jpg';  // headers to send your file header("Content-Type: application/jpeg"); header("Content-Length: " . filesize($original_filename)); header('Content-Disposition: attachment; filename="' . $new_filename . '"');  // upload the file to the user and quit readfile($original_filename); exit; 

Hope it helps!

like image 30
Frankie Avatar answered Sep 25 '22 01:09

Frankie