What could be done to workaround an archive corrupted by using codeigniter force_download?

Hi Experts!

Unfortunatelly the Codeigneiter's  force_download function corrupts the archive downloaded, na e-book.

      
          $data = file_get_contents(base_url('xxxxx/xxxxx/e_pub/' . $arquivo));
          force_download($arquivo, $data);

Open in new window


If I do the download it by FTP the archive is OK.

What must be done to workaround it?

Thanks in advance
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

skijCommented:
Try this:
$file = base_url('xxxxx/xxxxx/e_pub/' . $arquivo);
if (file_exists($file)) {
 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Disposition: attachment;');
 header('Content-Length: ' . filesize($file));
 readfile($file);
 exit;
}
else {
 echo 'Cannot find file.  Check path: ' . $file;
 exit;
}

Open in new window

Eduardo FuerteDeveloper and AnalystAuthor Commented:
I tryed  this function and  Ray's: force_download obtained here before.

Unfortunatelly the e-book that is downloaded from our PHP site is always corrupted, the same way than with codeigniter's force_download...

I'm going to try it outside Codeigniter to see what happens...
Julian HansenCommented:
I suspect the problem is that you are already doing some output prior to doing the force download.

Is it possible to verify that you are not doing any output to the browser prior to issuing the force_download - or moving the function call to somewhere in your code where you know that no output has been sent.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

skijCommented:
Try this:
header('Location: ' . base_url('xxxxx/xxxxx/e_pub/' . $arquivo) ,TRUE,307);
exit;

Open in new window

Does that redirect to a valid path?
Eduardo FuerteDeveloper and AnalystAuthor Commented:
The path is always OK and the download was done but the file is corrupted.

I  put  the code you suggeted  before Codeigniter's  force_download.

The purpose is to  "clean"  outputs before downloads?

I'm testing... the process became very slowly..
Eduardo FuerteDeveloper and AnalystAuthor Commented:
In fact the code

    header('Location: ' . base_url('xxxxxx/xxxxx/e_pub/'  . $arquivo) ,TRUE,307);

Open in new window


Apparently inviabilize the work of  the rest.

    $data = file_get_contents(base_url('xxxxx/xxxxx/e_pub/' . $arquivo));
    $oldvalue = ini_set('memory_limit', '512M');
    force_download($arquivo, $data);

Open in new window


And the download doesn't run.
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Just this finally runs !!!

Instead of force download...

redirect(base_url('xxxxx/xxxxx/e_pub/' . $arquivo));
Julian HansenCommented:
Are you able to open the downloaded file with a text editor and see what is at the start of the file.

I am pretty sure this is a problem with output happening before the download.
skijCommented:
It is also possibly a compression issue.  The server and/or PHP can compress data before sending it.
http://php.net/manual/en/function.gzcompress.php
http://php.net/manual/en/function.gzencode.php
http://httpd.apache.org/docs/2.2/mod/mod_deflate.html

If the headers to do not match the compression method, corruption can result.
Using redirect() makes the previous compression related headers irrelevant.
Eduardo FuerteDeveloper and AnalystAuthor Commented:
redirect() simple solved the problem. No needs of memory_limit increases too.
skijCommented:
Do you require additional help?

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
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.