gpt4 book ai didi

php - PHP 中的 ZipArchive 问题 "Error 21 - Is a Directory"

转载 作者:可可西里 更新时间:2023-10-31 22:51:21 24 4
gpt4 key购买 nike

我正在生成一组 HTML、CSS 和图像文件,并使用 ZipArchive 将它们压缩成一个 zip 文件。我已确认生成的 Assets 有效,但当我尝试压缩文件集时,生成的存档文件无法打开。

我在 PHP 中没有收到任何错误,当我 echo $zip->close() 它返回 true 时,我认为这意味着它能够写入并保存文件没有问题。使用 mac Archive Utility 打开 zip 会引发此错误:

"Unable to expand "filename.zip" into "Downloads". (Error 21 - Is a directory.)

这里可能出了什么问题?

这是完整的 PHP 脚本:

<?php
$ref = $_SERVER["HTTP_REFERER"];
$html = $_REQUEST['html'];
$images = $_REQUEST['images'];

$folder = uniqid();
$prepped = str_replace($ref.'server/php/files/', 'images/', $html);

mkdir("./runways/$folder", 0777);
mkdir("./runways/$folder/images", 0777);
mkdir("./runways/$folder/css", 0777);


file_put_contents('./runways/'.$folder.'/index.html',$prepped);
copy('../../css/runway.css', './runways/'.$folder.'/css/runway.css');

foreach($images as $image) {
$i = urldecode(str_replace($ref.'server/php/files/', '', $image));
$idata = file_get_contents('./files/'.$i);
file_put_contents('./runways/'.$folder.'/images/'.$i, $idata);

}

//echo $ref.'server/php/runways/'.$folder.'/';

$sourcefolder = './runways/'.$folder.'/';
$zipfilename = $folder.'.zip';

$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);

ini_set('max_execution_time', 5000);
$zip = new ZipArchive();

if ($zip->open('./zips/'.$zipfilename, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}

foreach ($filelist as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
$zip->close();

echo $ref.'server/php/zips/'.$zipfilename;

?>

最佳答案

您正在创建的存档包括当前工作目录和父目录(文件名“.”和“..”),您应该将其省略。例如,如果您查看原始代码创建的存档的内容,您将看到如下内容:

$ unzip -l 54b69fbd2de29.zip 
Archive: 54b69fbd2de29.zip
Length Date Time Name
-------- ---- ---- ----
0 01-14-15 09:56 ./runways/54b69fbd2de29/.
0 01-14-15 09:56 ./runways/54b69fbd2de29/..
0 01-14-15 09:56 ./runways/54b69fbd2de29/css/.
0 01-14-15 09:56 ./runways/54b69fbd2de29/css/..
12 01-14-15 09:56 ./runways/54b69fbd2de29/css/runway.css
0 01-14-15 09:56 ./runways/54b69fbd2de29/images/.
0 01-14-15 09:56 ./runways/54b69fbd2de29/images/..
26 01-14-15 09:56 ./runways/54b69fbd2de29/images/image1.jpg
31 01-14-15 09:56 ./runways/54b69fbd2de29/images/image2.jpg
6 01-14-15 09:56 ./runways/54b69fbd2de29/index.html
-------- -------
75 10 files

你不想要“./runways/54b69fbd2de29/”。或“./runways/54b69fbd2de29/..”等。这是解决此问题的一种方法,将最终的 foreach 更改为以下内容(另请注意,您不需要 $key=>$value,只需 $key):

foreach ($filelist as $key) {
if (!preg_match('/\/\.{1,2}$/',$key)){
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
}

生成的存档是有效的,看起来像这样:

unzip -l 54b6a39d55a63.zip 
Archive: 54b6a39d55a63.zip
Length Date Time Name
-------- ---- ---- ----
12 01-14-15 10:13 ./runways/54b6a39d55a63/css/runway.css
26 01-14-15 10:13 ./runways/54b6a39d55a63/images/image1.jpg
31 01-14-15 10:13 ./runways/54b6a39d55a63/images/image2.jpg
6 01-14-15 10:13 ./runways/54b6a39d55a63/index.html
-------- -------
75 4 files

关于php - PHP 中的 ZipArchive 问题 "Error 21 - Is a Directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23771214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com