Link to home
Start Free TrialLog in
Avatar of Georgiana Gligor
Georgiana Gligor

asked on

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
Avatar of syrma
syrma

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.
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();
       
   }
Avatar of Georgiana Gligor

ASKER

hi,

sad to say, but it ain't workin'

GB
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.
:) 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
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
ASKER CERTIFIED SOLUTION
Avatar of syrma
syrma

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
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
I'm happy you found the solution.
Thanks for the points :)