php ziparchive留下未完成的zip文件

时间:2014-06-12 12:31:52

标签: php iis web zip ziparchive

我正在尝试使用php创建三个zip包。我正在运行一个复制目录并查看内容的函数,将每个文件添加一个循环到zip中。

问题是ZipArchive在完成时似乎没有关闭文件。 我最终想要的是这样的三个zip文件:

Package1.zip
Package2.zip
Package3.zip

相反,我最终会得到类似的东西:

Package1.zip.a012341
Package1.zip.b012342
Package2.zip.a023451
Package2.zip.b023452
Package3.zip.a034561
Package3.zip.b034562

就像zip文件没有关闭并完成删除临时扩展并试图创建另一个文件而失败了。我没有得到任何错误,至少不是我见过的。

我猜测的是我需要在php.ini或我的网络服务器/ IIS上更改某种设置。我正在尝试创建的文件非常大,大约在450到600 MB之间。

这是我最好的猜测,因为我有两个几乎相同的服务器设置,我尝试了相同的代码。它完美地适用于其中一个而不是另一个。这证明我的ZipArchive代码不能解决问题(我没有设置服务器顺便说一句,所以我不确定有什么不同)。

我试图在php.ini中增加memory_limit和执行时间。后者是较早的问题,我收到一条消息告诉我它已经超时了。

提前致谢!

编辑:添加了该功能。

function package_site($filepath, &$context) {
    $context['message'] = 'Creating package';

    // Mac app
    $mac_zipfile = 'public://site_export/mac_package.zip';
    copy_directory($mac_zipfile, $filepath . '/mac_package.zip', '/.*/');
    $files = file_scan_directory($filepath . '/files', '/.*/');

    $mac_zip = new ZipArchive();
    if($mac_zip->open(drupal_realpath($filepath . '/mac_package.zip')) === true) {
        foreach($files as $key => $value) {
            $mac_zip->addFile(drupal_realpath($value->uri), 'mac_package.app/Contents/Resources/' . substr(drupal_realpath($value->uri), strlen(drupal_realpath($filepath . '/files')) + 1));
        }
    } else {
        drupal_set_message("Could NOT open mac_package.zip");
    }
    $mac_zip->close();

    // PC app
    drupal_unlink($filepath . '/package.zip');
    $start_files_path_pc = 'public://site_export/pc_files';
    copy_directory($start_files_path_pc, $filepath . '/files', '/.*/');
    $pc_files = file_scan_directory($filepath . '/files', '/.*/');

    $zip = new ZipArchive();
    $res = $zip->open(drupal_realpath($filepath . '/package.zip'), ZipArchive::CREATE);
    if ($res === TRUE) {
        foreach ($pc_files as $key => $value) {
            $zip->addFile(drupal_realpath($value->uri), substr(drupal_realpath($value->uri), strlen(drupal_realpath($filepath . '/files')) + 1));
        }
        $zip->close();
    }

    // Create a html package
    create_pure_html_package($filepath);

    // HTML package
    drupal_unlink($filepath . '/html.zip');
    $html_files = file_scan_directory($filepath . '/files/site', '/.*/');

    $zip_html = new ZipArchive();
    $res = $zip_html->open(drupal_realpath($filepath . '/html.zip'), ZipArchive::CREATE);
    if ($res === TRUE) {
        foreach ($html_files as $key => $value) {
            $zip_html->addFile(drupal_realpath($value->uri), substr(drupal_realpath($value->uri), strlen(drupal_realpath($filepath . '/files')) + 1));
        }
        $zip_html->addFile(drupal_realpath($filepath . '/start.html'), 'start.html');
        $zip_html->close();
    }

    rrmdir(drupal_realpath($filepath . '/files'));
    unlink(drupal_realpath($filepath . '/start.html'));
}

1 个答案:

答案 0 :(得分:0)

我刚才遇到了同样的问题。我通过更改文件目录权限(到非readonly)解决了这个问题。这是我的答案,我希望它对你有帮助!