PDA

View Full Version : Download header cause zip file to be corrupt


tsclan
10-23-2005, 01:25 AM
I made a script that will upload a .zip file and rename it based on the id of the information that was inserted into a database. All works fine, except most of the files when you download them they say they are corrupted, yet you can extract them and everything works fine. Any ideas on why it would say it was corrupted..

here is code...

upload


foreach($_FILES as $file_name => $file_array) {
$ext = explode(".", $file_array['name']);
$ext1 = $ext[1];
if ($ext1 == "zip") {
if ($file_array['size'] < $maxzip) {
move_uploaded_file($file_array['tmp_name'],$template_dir . $id.".".$ext1);
} else {
$temp->assign('error', "Zip Files Must Not Exceed 5 MegaBytes!");
$temp->display('error.tpl');
$temp->display('submit_templates.tpl');
exit();
}
} else {
move_uploaded_file($file_array['tmp_name'],$thumbnail_dir . $id.".".$ext1);
}



download

$filename = $template_dir . $file."."."zip";
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");

Element
10-23-2005, 02:19 AM
I made a script that will upload a .zip file and rename it based on the id of the information that was inserted into a database. All works fine, except most of the files when you download them they say they are corrupted, yet you can extract them and everything works fine. Any ideas on why it would say it was corrupted..

here is code...

upload


foreach($_FILES as $file_name => $file_array) {
$ext = explode(".", $file_array['name']);
$ext1 = $ext[1];
if ($ext1 == "zip") {
if ($file_array['size'] < $maxzip) {
move_uploaded_file($file_array['tmp_name'],$template_dir . $id.".".$ext1);
} else {
$temp->assign('error', "Zip Files Must Not Exceed 5 MegaBytes!");
$temp->display('error.tpl');
$temp->display('submit_templates.tpl');
exit();
}
} else {
move_uploaded_file($file_array['tmp_name'],$thumbnail_dir . $id.".".$ext1);
}



download

$filename = $template_dir . $file."."."zip";
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");

Try
$filename = $template_dir . $file."."."zip";
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");

tsclan
10-23-2005, 02:57 PM
Thank you.
After reading through the PHP manual on headers http://us3.php.net/header I noticed that if you are going to have the same type of headers then the next one cancels out the first one unless you put in FALSE. So it should now look like this


$filename = $template_dir . $file."."."zip";
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream", FALSE);
header("Content-Type: application/download", FALSE);
header("Content-Disposition: attachment; filename=".basename($filename));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");

felgall
10-23-2005, 09:55 PM
If they are ZIP files shouldn't you use application/x-zip-compressed instead of application/octet-stream?