5
私はフォルダとファイルをPHPで圧縮していますが、zipファイルを開こうとすると代わりにcpgzファイルが表示されます。そのファイルを解凍した後、私は別のzipファイルを取得します。それは、現在のフォルダをベーススキャンしてファイルとフォルダをスキャンして圧縮することです。 これは私が使用するコードです:PHPで圧縮されたファイルは、抽出後にcpgzファイルになります
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は、アプリケーションコードと同じサーバー上のzipファイルですか?私は私が[ここで解決した]まったく同じ問題を抱えていました(http://stackoverflow.com/questions/11495103/opening-downloaded-zip-file-creates-cpgz-file)。また、あなたは 'header() 'のように見えますか? –