48

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.

Brad Koch
20.5k20 gold badges114 silver badges141 bronze badges
asked Oct 27, 2009 at 1:17
1
  • You can also do this in Apache with mod_rewrite without needing to hit your PHP scripts. May not be applicable to your situation, but if it is, it could improve performance. Commented Oct 27, 2009 at 1:34

5 Answers 5

78

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"');
Toby Allen
11.3k12 gold badges80 silver badges132 bronze badges
answered Oct 27, 2009 at 1:21
Sign up to request clarification or add additional context in comments.

3 Comments

yes i can see now, thx :), but i guess i must also use "readfile('original.pdf');" to do it
Absolutely, and good to set the Content-Length and Content-Type too. You can use filesize() for the length and write your own function based on the extension to do content-type or use uk2.php.net/fileinfo
@RULE101 you can either use readfile() or fread() to do it
33

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!

answered Oct 27, 2009 at 1:25

Comments

30

There is also another way if you are using html5

<a href="link/to/my/file/with/a/name/i/dont/like.jpg" download="MyFile.jpg">Download</a>

Cheers :3

answered Sep 20, 2015 at 7:09

2 Comments

download attribute is not work in safari, this is only work on crome ,firefox
Works for me. No one's using safari anymore. At least no one who's serious about browsing the web...
3

Nothing wrong with the above but I had to add:

ob_clean();
flush();

before readfile

otherwise I can not open the download jpg/png file

Rui Jarimba
18.4k11 gold badges64 silver badges101 bronze badges
answered Aug 15, 2013 at 12:33

1 Comment

Very usefull. Without it I was getting a corrupted file.
3
<a href="4324ffsd34.jpg" download="filetodownload">Download</a>
answered Sep 30, 2017 at 11:55

2 Comments

Note you don't have to specify file extension, it will be added by the browser.
This "download" attribute is only honored for links to resources with the same-origin.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.