PHP Force uploads corrupted file
Hi, I am currently working on some php-zend framework project on my osx-apache. The problem is when ever I want to force a download of some files using my php app, the uploaded files will get corrupted and the file size is 5.4 kb! I tried so many changes in my code and even used some classes to force loading, but the problem remains the same! I have to say that I have used force loading in the actions of my controllers. Does overwriting something or something like that affect file upload ?!
This is the basic 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 use are: 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();*/
source to share
Ok, I found the problem using the last comment from dcaunt . Actually I just look at the binary source of the output file and noticed that there is a small character and a space on a newline on the first line, so by removing them the file reverts to normal and becomes readable. So to strip out those spaces, I decided to remove the end of my php script tag "?>" In my controller, and this makes php not post them to the output content. Any thanks to everyone for your comments :)
source to share
I highly recommend using this Action Helper to send files - it can send data from memory or file to disk and makes it easy to set caching and content options
Despite the name SendFile, apache sendfile is optional.
source to share