views:

1254

answers:

3

Hi I'm currently working on some php - zend framework project on my osx - apache. The problem is when ever I want to force the download of some files using my php application the downloaded files is corrupted and the size of the file is 5.4 kb! I've tried so many changes in my code and even used some classes to force the download but still the problem is the same! I should say I used the force download in one my controllers' actions. Does the rewrite or something likes this has affect over the downloading of the file ?!

This is the base code :

header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Disposition: attachment; filename='.basename($file["files_url"]));
 header('Content-Transfer-Encoding: binary');
 header('Expires: 0');
 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
 header('Pragma: public');
 header('Content-Length: ' . filesize($file["files_url"]));
 ob_clean();
 flush();
 readfile($file["files_url"]);
 exit;*/

And the Classes I used : BF_Download

$download = new Download($file["files_url"],$file["files_title"],"on",20);
 $download->download_file();

And :

$zip = new zip_file("../".$file["files_title"].".zip");

 $zip->set_options(array('inmemory' => 1, 'recurse' => 0, 'storepaths' => 0));
 $zip->add_files($file["files_url"]);
 $zip->create_archive();
 $zip->download_file();*/
+2  A: 

Try viewing the file as binary data on the webpage. What could be happening is that some PHP error is sneaking into the output and corrupting it.

So skip the content type headers and just look at the pure data.

If you're still not seeing it, keep it in tact and look at your error log.

Ólafur Waage
There seems to be no php error, and even the binary view is working ( I can't tell that's is correct or not, but undoubtedly it's the binary view of file and not a php error). The php and apache error logs dose not show any errors neither.
Farid
A: 

I strongly recommend using this Action Helper for sending files - it can send data from memory or a file on disk and makes it easy to set options for caching and content

Despite the name SendFile, apache sendfile is not a requirement.

David Caunt
Thanks for your comment, I used the helper, but the result was the same! still the downloaded file is corrupted!
Farid
A: 

Ok, I found the problem by the help of the last comment from the dcaunt, Actually I just take look at the binary source of the output file and noticed that there is tiny newline character and space at the first line, so by removing them, the file return to it's normal state and it's become readable. So in order to destroy those spaces I decieded to remove the end of my php script tag " ?> " in my controller, and that causes the php not to send them in to the content of output. Any thank you all for your comments :)

Farid
good find! been there, done that (and wasted a _lot_ of time!)
Steve