Jump to content

Recommended Posts

So to stop third-party traffic on certain media files my site does, I did a download.php type script that just took in the media id, and found the filename and did a readfile on it for the user.

The problem is that when you start doing massive amounts of traffic with it, I think the readfile stream is taking up a lot of cpu. Is there any alternative to readfile that creates a direct link between the user and the file, instead of it having to go through the php?

I'm afraid that this is something I need to do on the server end, and not in PHP, but I figured I'd ask.
Link to comment
https://forums.phpfreaks.com/topic/9532-alternative-to-readfile/
Share on other sites

from php.net/readfile comments section:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
A mime-type-independent forced download can also be conducted by using:

<?
(...)
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/x-download");
header("Content-Disposition: attachment; filename={$new_name}");
header("Content-Transfer-Encoding: binary");
?>
[/quote]

i don't know if this will help you in any way. if you go to php.net/readfile and scroll down to the submitted comments it gives various alternate methods for forced downloads.
Link to comment
https://forums.phpfreaks.com/topic/9532-alternative-to-readfile/#findComment-35193
Share on other sites

You missunderstand. I know how to do the code with readfile - I'm looking for a way to make the connection between media server and client directly, without piping it through php.

MEDIA SERVER --> readfile() --> user
-bad for high traffic - effectively doubles your bandwith usage

MEDIA SERVER --> user
-happiness!
Link to comment
https://forums.phpfreaks.com/topic/9532-alternative-to-readfile/#findComment-35342
Share on other sites

  • 3 years later...
You can use CV's code
[quote author=Crayon Violent link=topic=93268.msg373213#msg373213 date=1147333559]
[code]
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/x-download");
header("Content-Disposition: attachment; filename=\"{$new_name}\"");
header("Content-Transfer-Encoding: binary");
?>
[/code]
[/quote]
Just set $new_name to the name and Location to the url
ie
[code=php:0]
header("Location: http://mydomain.com/myfile.txt");
$new_name = "myfile.txt";[/code]
  • 2 years later...
I'm writing because I see that this thread, even though old, it still is pretty up-to-date with the situation.

The readfile method works perfectly, however, I need a way to force the download on the user and make them download the file straight from the external server.  Whenever I am trying to use:

header('Content-Disposition:attachment;bla bla bla without readfile

I download the file, however it is 0 bytes. I then thought that maybe the browser did not understand how many bytes to download, so I calculated the file size of the remote file and sent another header along the way like:

header("Content-length: $video_size");

Has anyone managed to successfully got this to work without the use of readfile and not get an empty zero file?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.