• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Offer a large file for download

Hi,

I have a problem in providing very large (few MB) files for the users to download.
Here is what I am currently using, and I need to note that this is working for small files:

<?php
/**
 * Note: the content type is hardcoded because we only need this particular filetype.
 *
 * @param string $sFile full path to the file
 */
function downloadFile( $sFile ){
  header( 'Content-type: application/vnd.ms-excel' );
  header( 'Content-Disposition: attachment; filename="' . $sFile . '"' );

  set_time_limit( 0 );
  readfile( $sFile );
  exit;
?>

Thanks in advance for help,
  GB
0
Georgiana Gligor
Asked:
Georgiana Gligor
  • 7
  • 5
1 Solution
 
syrmaCommented:
Dont' use readfile() for big files.
Read file in smaller chunks to a buffer variable whith fread().
In this way the amount of memory needed is based on your buffer size.
0
 
syrmaCommented:
Here is a code for this from php.net

header('Content-Type: application/force-download');
header ("Content-Length: " . filesize($file));
header ("Content-Disposition: attachment; filename=$theFileName");

   $fd = fopen($file, "r");
   while(!feof($fd))
  {
       echo fread($fd, 4096);
       ob_flush();
       
   }
0
 
syrmaCommented:
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Georgiana GligorAuthor Commented:
hi,

sad to say, but it ain't workin'

GB
0
 
syrmaCommented:
did  you increase the execution time as in the link:

ini_set("max_execution_time", 9999);

please put with:

error_reporting(E_ALL);

in the beginning of the script to see what exact error message you have.
0
 
Georgiana GligorAuthor Commented:
:) I am using a set_time_limit( 0 ); And error reporting _is_ set to E_ALL, but there is no error.

My browser just says :
* IE > 404 response
* Firefox > offers me the script.php for download instead of the file.xls I am expecting.

Cheers,
  GB
0
 
Georgiana GligorAuthor Commented:
The entire script does the following:

1 - generates an Excel file using the PEAR extension.
2 - offers the file for user to locally save.

the 1st part works great, yet the 2nd refuses.
I get information in batch of 50 entries from the DB, which I write sequentially to the file.
After I close everything, I want to be able to download it, not keep it remotely, and this is the part where I have difficulties.
It doesn't work even for 400 KB files sometimes. So the x xxx MB part is still a nice dream.

GB
0
 
syrmaCommented:
I just checked this with 7 Mb file and it is working fine:

<?php
error_reporting(E_ALL);
set_time_limit( 0 );

$file="test.xls";

header('Content-Type: application/force-download');
header ("Content-Length: " . filesize($file));
header ("Content-Disposition: attachment; filename=".basename($file));

   $fd = fopen($file, "r");
   while(!feof($fd))
  {
       echo fread($fd, 4096);
       ob_flush();
       
   }

?>

I think it's better at this point to check your path to the file and the file itself, try with different file for example, not generated from the 1st script; It might be some not valid data in the file itself.
0
 
syrmaCommented:
Please check for quotes as well, maybe there is some not properly handled quote around the 400th kB which is stopping you to procede. This doesn't look to me as a file size limitation any more.
0
 
Georgiana GligorAuthor Commented:
First of all, there is a single script, no 2 different ones for the 2 operations described.
The file gets generated correctly on the server, I have got it locally and tested both on Microsoft Windows Excel and Linux OpenOffice (hence it is valid data in there), yet it is not presented to the user as a valid download.
As it's an Excel file, I have discovered I have to open it in 'rb' mode rather than just 'r' mode.
Also, if it's an Excel file, with data dynamically generated from a DB (and I mean here a highly DB-intensive script), how can I safely avoid any quotes problem ?

Regards,
  GB
0
 
Georgiana GligorAuthor Commented:
Finally accepted your answer because the issue was splitting the code into 2 different scripts.

Even if I have some redirection issues at this time, it's okay from the download file point of view.

Many thanks for assistance,
  GB
0
 
syrmaCommented:
I'm happy you found the solution.
Thanks for the points :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now