The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. December 05, 2008, 10:08:43 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Returning a huge file to a browser via PHP  (Read 259 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« on: June 15, 2008, 04:07:09 PM »

This might not be entirely obvious, but if you run requests for downloadable files through a PHP routine, then you basically need to load the file and echo it back to the user.

Why would you run a download through PHP you may ask?
What if you wanted to either track or deny the download programmatically?
Or perhaps send a different file back to a different type of surfer Mobster


Anyhoo, the unforseen problem can be that if the to-be-downloaded file is huge, then loading it up into memory and squirting it back out to the user may fail because you've exceeded PHP's available memory. Here's how it can be done: (Note that you'd probably get the name/type of this file by other means, like from a DB or the tail of the file, which is immaterial to the example - also, the types of files should be expanded and made more robust - this is just to spin your gears)

Code:
<?php
$theFile 
'afile.tar.gz';
$theType 'tar.gz';

switch (
$theType)
{
case 'zip':
$appType 'application/zip';
break;
case 'txt':
$appType 'text/plain';
break;
case 'tar.gz':
$appType 'application/x-gzip';
break;
case 'pdf':
$appType 'application/pdf';
break;
}

header('Accept-Ranges: bytes');
header('Content-Length: ' filesize($theFile));
header("Content-Type: content-type=$appType");

$f fopen($theFile'r');
while (
$buff fread($f8192)) echo($buff);
fclose($f);

?>


Hope that helps,

/p
« Last Edit: June 15, 2008, 04:11:58 PM by perkiset » Logged

If I can't be Mr. Root then I don't want to play.
thedarkness
Global Moderator
Lifer
*****
Offline Offline

Posts: 581



View Profile
« Reply #1 on: June 15, 2008, 09:08:59 PM »

This may interest you perk, if we're talking about large files:

http://au.php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #2 on: June 15, 2008, 09:13:46 PM »

Absolutely TD , appreciate it... in this case, it's more about appearing completely normal than it is compression and efficiency, if you catch my drift... Wink

I do compression in several situations and it works pretty nicely.
Logged

If I can't be Mr. Root then I don't want to play.
thedarkness
Global Moderator
Lifer
*****
Offline Offline

Posts: 581



View Profile
« Reply #3 on: June 15, 2008, 11:14:24 PM »

coolio
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!