Jump to content

Archived

This topic is now archived and is closed to further replies.

Zubaz

Alternative to readfile()

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.

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
A media server is a computer appliance, ranging from an enterprise class machine providing video on demand, to, more commonly,

________________
[url=http://www.inin.com/ProductSolutions/Pages/Enterprise-Interaction-Center.aspx]IP PBX[/url]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
does not matter what way you do this your need good bandwidth and a dedicated server for the media files if there thousands like torents.

also all users should be logged in to download the files more secure.

go the ftp way better and more secure.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

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.