Jump to content


Photo

Alternative to readfile()


  • Please log in to reply
8 replies to this topic

#1 Zubaz

Zubaz
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 11 May 2006 - 07:36 AM

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.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 11 May 2006 - 07:45 AM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Zubaz

Zubaz
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 13 May 2006 - 12:47 AM

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!

#4 fuji0050

fuji0050
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 28 May 2009 - 10:07 AM

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

________________
IP PBX

#5 MadTechie

MadTechie
  • Staff Alumni
  • Advanced Member
  • 9,404 posts
  • LocationUK

Posted 28 May 2009 - 10:11 AM

You can use CV's 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");
?>

Just set $new_name to the name and Location to the url
ie
header("Location: http://mydomain.com/myfile.txt");
$new_name = "myfile.txt";

Computers are good at following instructions, but not at reading your mind.

The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato

I dunno about that. A regular expression has a 0% chance of touching my penis.

the code is professionally made up but not working

Remember to Click Solved, how to ask questions - the smart way

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 28 May 2009 - 10:14 AM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 28 May 2009 - 01:10 PM

seriously? this thread is 2 years old...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 MadTechie

MadTechie
  • Staff Alumni
  • Advanced Member
  • 9,404 posts
  • LocationUK

Posted 28 May 2009 - 01:27 PM

LOL didn't notice, fuji0050 reply revived It
Computers are good at following instructions, but not at reading your mind.

The quality of a response, is usually directly related to the quality of the question. ©2009 mjdamato

I dunno about that. A regular expression has a 0% chance of touching my penis.

the code is professionally made up but not working

Remember to Click Solved, how to ask questions - the smart way

#9 demonicoder

demonicoder
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 21 December 2011 - 11:30 AM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users