/    Sign up×
Community /Pin to ProfileBookmark

Force download and filenames

Here is my code that forces the download of a mp3- file from another server.

[CODE]<?php
$mp3 = “http://www.mysite.com/song.mp3”;
header(“Content-Disposition: attachment; filename=$mp3”);
header(“Content-Type: audio/mpeg”);
readfile($mp3);
?>[/CODE]

The problem is that the filename is the entire URL when downloading this file, and I want it to be shorter.
(e.g. http___www.mysite.com__song.mp3″). Can that be changed to just “song.mp3” or any other value?

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJun 29.2009 — [code=php]
$filename = '/foo/bar/baz/file.mp3';
echo basename($filename); // file.mp3
[/code]


If your filenames are not filenames but URLs as in your example, you can use parse_url.
Copy linkTweet thisAlerts:
@CeoxauthorJun 29.2009 — So I replace readfile() with basename() or parse_url() ?
Copy linkTweet thisAlerts:
@MindzaiJun 29.2009 — No, they do completely different things. I thought your original question was (essentially), how to extract a filename from a path. This is what basename does. Then I noticed you example where you want to extract it from a URL, in which case parse_url will get you the filename.
Copy linkTweet thisAlerts:
@CeoxauthorJun 29.2009 — I know how to extract a filename from a path or URL.

I'll explain this better. That code should force the download of song.mp3 on another server (mysite.com). That code results in a pop-up dialog in Firefox that asks what you wish to do with the file (open it with a program, save it on the hard drive). In this dialog, the filename is set automatically to be "http___mysite.com_song.php" (because that's the url of the file). I wanted to change this filename to something simpler, but since the force download is done using headers I can't just extract the filename (song.mp3) from the URL (http://www.mysite.com), because then the script would be looking for the file on the local server, right?
Copy linkTweet thisAlerts:
@edhardyJun 30.2009 — it is very good
Copy linkTweet thisAlerts:
@JunkMaleJun 30.2009 — @CEOX

You can not force people to download a file that they chose to open or are forced by browser setting to open (play).

The pop up dialogue will happen because the end user has not chosen a default action like "Download" the file or "Play in default player", this is normal and something that you have not got control over.

As for your issue with the filename, this has been given to you, basename($mp3) will return the name of the file minus the URL bit.
Copy linkTweet thisAlerts:
@MindzaiJun 30.2009 — You can force a browser to offer a download dialog instead of handling the file itself by using the correct headers. However I'm not sure how this is relevant? The OP has already stated that they are forcing the download and that isn't the problem.

As far as I can understand, the problem is the filename as displayed in the download dialog - is this correct? I'm not sure of how you could do that without copying the file to the local server and changing the filename before serving it to the client (which is a pretty ridiculous solution). I'm not aware of any other way of altering this without breaking the header URL, but I'd be interested to hear if anyone knows of a way.
Copy linkTweet thisAlerts:
@CeoxauthorJun 30.2009 — 
As far as I can understand, the problem is the filename as displayed in the download dialog - is this correct?[/QUOTE]

Exactly.

I'm not sure of how you could do that without copying the file to the local server and changing the filename before serving it to the client (which is a pretty ridiculous solution). I'm not aware of any other way of altering this without breaking the header URL, but I'd be interested to hear if anyone knows of a way.
[/QUOTE]

I thought about copying too, but yes it wouldn't be a good solution.

And yes I can force the download of a file. That script does that.
Copy linkTweet thisAlerts:
@JunkMaleJun 30.2009 — Copying? Why? The file is on the server, all you need to do is send the header with the filename you want and then allow the user to select what they want to do with the stream.
Copy linkTweet thisAlerts:
@CeoxauthorJun 30.2009 — But the filename in the download dialog is long, which I want shorter. That's what this thread is all about.
Copy linkTweet thisAlerts:
@JunkMaleJun 30.2009 — And you have been told that answer is to use basename( $whateveryourfilenamevariablehappensotbe ); to do that.

[code=php]echo basename($filename); // file.mp3[/code]

Which is what Mindzai was demonstrating in the above section, the output from that would just be the name of the file and extention.
×

Success!

Help @Ceox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...