简体   繁体   English

PHP 创建的 ZIP 文件不适用于 Windows 资源管理器

[英]PHP created ZIP file not working with Windows Explorer

I have the following code which works fine on windows with WinRAR and works fine on Mac's as well.我有以下代码,在 windows 和 WinRAR 上运行良好,在 Mac 上也运行良好。 However, for some reason, when you open it with the default windows Explorer, the zip appears empty and when you right click and extract, it says it is invalid.但是,由于某种原因,当您使用默认的 windows 资源管理器打开它时,zip 显示为空,当您右键单击并解压缩时,它说它无效。 When the same one is opened with winrar or on a mac, all the files are there.当用winrar或mac打开同一个文件时,所有文件都在那里。 Any ideas?有任何想法吗?

$passcode = $_GET['passcode'];

    $zip = new ZipArchive;
    $download = 'download_files_'.$passcode.'.zip';
    $zip->open($download, ZipArchive::CREATE);
    foreach (glob("../dashboard/uploads/".$passcode."/*.jpg") as $file) { /* Add appropriate path to read content of zip */
        $zip->addFile($file);
    }
    $zip->close();
    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename = $download");
    header('Content-Length: ' . filesize($download));
    header("Location: $download");

You can solve this issue using following method. 您可以使用以下方法解决此问题。

foreach (glob("../dashboard/uploads/".$passcode."/*.jpg") as $file) { 
    $zip->addFile(realpath($file), pathinfo($file, PATHINFO_BASENAME));
}

I hope this helps you. 我希望这可以帮助你。

通过使用以下方法可以很容易地解决这个问题:我必须将生成的zip文件移动到uploads文件夹中并删除../dashboard/uploads/ ,因为这条路径导致Windows将其视为损坏的文件

just my $0.02 - Windows file manager doesn't like, if files in ZIP archives are stored with leading delimiter. 只是我的0.02美元 - Windows文件管理器不喜欢,如果ZIP存档中的文件存储有前导分隔符。

This doesn't work: 这不起作用:

$zip->addFile(realpath($file), '/mydir/myfile.txt');

this works: 这工作:

$zip->addFile(realpath($file), 'mydir/myfile.txt');

Another one related fix:另一个相关的修复:

Linux is able to read a zip file with some html at the begin, you can use ob_clean() to clear unwanted code sent to the default output. Linux is able to read a zip file with some html at the begin, you can use ob_clean() to clear unwanted code sent to the default output.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM