ziparchive创建空的zip文件夹

时间:2014-12-27 14:39:25

标签: php zip

我创建了一个文件拉链。用一些文件夹和索引文件创建zip。 我通过以下代码下载zip文件,没有任何错误,但下载的zip文件为空。 这是我的zip存档脚本的问题吗? 它在我的localhost中工作得很好。但它不在服务器上工作。

/* CONFIG */

$pathToAssets = array( "elements/css", "elements/fonts", "elements/images", "elements/js");

$filename = "page.zip";

/* END CONFIG */


$zip = new ZipArchive();

$zip->open($filename, ZipArchive::CREATE);


//add folder structure

foreach( $pathToAssets as $thePath ) {

    // Create recursive directory iterator
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator( $thePath ),
        RecursiveIteratorIterator::LEAVES_ONLY
    );

    foreach ($files as $name => $file) {

        if( $file->getFilename() != '.' && $file->getFilename() != '..' ) {

            // Get real path for current file
            $filePath = $file->getRealPath();

            $temp = explode("/", $name);

            array_shift( $temp );

            $newName = implode("/", $temp);

            // Add current file to archive
            $zip->addFile($filePath, $newName);

        }

    }

}



foreach( $_POST['pages'] as $page=>$content ) {

    $zip->addFromString($page.".html", $_POST['doctype']."\n".stripslashes($content));
}

$zip->close();


$yourfile = $filename;

$file_name = basename($yourfile);

header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));

readfile($yourfile);

unlink('folder.zip');

exit;

提前谢谢

0 个答案:

没有答案