Tôi đang nén thư mục và tệp bằng php, nhưng khi tôi cố gắng mở tệp zip, tôi sẽ nhận tệp cpgz thay thế. Sau khi giải nén tập tin đó tôi nhận được một tập tin zip. Những gì nó làm là bassicaly quét thư mục hiện tại cho các tập tin và thư mục để zip. Đây là mã tôi sử dụng:Tệp đã nén với kết quả PHP trong tệp cpgz sau khi trích xuất
function Zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = str_replace('\\', '/', realpath($file));
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
if($_GET["archive"]== 'true'){
$date = date("Ymd_Hi");
$dir = dirname(__FILE__);
$filename = $date.".zip";
Zip(getcwd(), $filename);
header("Content-disposition: attachment; filename=$filename");
header('Content-type: application/zip');
readfile($filename);
unlink($filename);
}
- @ Steven, là tệp zip của bạn trên cùng một máy chủ với mã ứng dụng của bạn? Tôi đã có cùng một vấn đề chính xác mà tôi [đã giải quyết ở đây] (http://stackoverflow.com/questions/11495103/opening-downloaded-zip-file-creates-cpgz-file). Ngoài ra, bạn 'header() 'trông như thế nào? –