/    Sign up×
Community /Pin to ProfileBookmark

download directly to client (remote download with filename)

I’m on a shared host that doesn’t allow Apache X-Sendfile.

I wrote a script to retrieve youtube mp4 urls, and I’d like to make a download for it that doesn’t have the name videoplayback.mp4, rather the video page title instead.

I was thinking about this

[code=php]header(“Content-Type: video/mp4”);
header(‘Content-Disposition: attachment; filename=”‘.basename($file_name).'”‘);
set_time_limit(0);
@ob_end_clean();
@ob_end_flush();
@readfile($mp4_url);
exit;[/code]

The problem is that it’s a little extreme just to give the download a name, because the file has to download to my host first and it has to process as a middle man the whole time during the download.

So it got me thinking about cURL. If return transfer is set to 0, like this:

[code=php]header(“Content-Type: video/mp4”);
header(‘Content-Disposition: attachment; filename=”‘.basename($file_name).'”‘);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$mp4_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
curl_exec($ch);
curl_close($ch);
exit;[/code]

Does it download directly to the client without going to the server first?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 08.2014 — Nope, it just means that as the PHP cURL transaction pulls data to your web server from the remote one, it immediately sends it to the output buffer. (No idea whether it waits until the whole file is received before it outputs it, or does it in chunks.)
Copy linkTweet thisAlerts:
@jacjil2authorOct 10.2014 — Ah. Well, that's too bad. I contacted support at my host about using X-Sendfile, and they said I would need to upgrade to VPS hosting. So poo...

I've kind of had this Youtube MP4 link generator idea on the shelf for a while, but just this morning I discovered that I don't need to run my host ragged with Readfile or Curl just to give the file a name.

I can just add "title=my movie name&" right after videoplayback? and it names the MP4 upon download. (unrelated to my topic title, but related to the purpose of my topic)
×

Success!

Help @jacjil2 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...