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
LVL 4
Georgiana GligorCTO / OwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Georgiana GligorCTO / OwnerAuthor 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 GligorCTO / OwnerAuthor 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 GligorCTO / OwnerAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 GligorCTO / OwnerAuthor 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 GligorCTO / OwnerAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.