When i trying to download the zip file with the following code in my local system,the file is downloading with all the files but where as in server it is downloading with 20 bytes and when extracts file is corrupted and no archive found.So i added some checks for this code and run but this time File not found,going inside this check(if (!is_file($archive_file_name))).What might be the problem on server?
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>
");
}
//add each files of $file_name array to archive
foreach($file_path as $file_path)
{
if(basename($file_path) =="documents")
{
foreach($file_names as $value=>$files)
{
$customer_files = $this->fetchCustomFieldValuesAndFieldNamesByOrder($files['order_id']);
if($customer_files['field_value'] !="" && file_exists($file_path.$customer_files['field_value']))
{
$file_extension = pathinfo($customer_files['field_value'], PATHINFO_EXTENSION);
$zip_file_name = $files['customer_name']."_"."Files.".$file_extension;
$zip->addFile($file_path.$customer_files['field_value'],$zip_file_name);
}
//echo $customer_files['field_value']."<br>";
}
} else
{
foreach($file_names as $value=>$files)
{
if($files['file_name'] !="" && file_exists($file_path.$files['file_name']))
{
$file_extension = pathinfo($files['file_name'], PATHINFO_EXTENSION);
$zip_file_name = $files['customer_name']."_"."Application".".$file_extension";
$zip->addFile($file_path.$files['file_name'],$zip_file_name);
// echo $file_path.$files['file_name'],$zip_file_name."<br>";
}
}
}
}
// echo "numfiles: " . $zip->numFiles . "
";
// echo "status:" . $zip->status . "
";
$zip->close();//exit;
//then send the headers to foce download the zip file
if (headers_sent()) {
echo 'HTTP header already sent';
} else {
if (!is_file($archive_file_name)) {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
echo 'File not found';
} else if (!is_readable($archive_file_name)) {
header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
echo 'File not readable';
} else {
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
}
}
If you're getting status 11 from $zip->status, it means that the file you're trying to ZIP does not exist. Here are docs on error codes from the Zip / ZipArchive class: http://php.net/manual/en/zip.constants.php
Verify that the path you're on is correct using getcwd() and that the file on this path really exists.