猿问

文件存在但收到无法打开流的警告:没有这样的文件或目录

<?php

$arch_filename = "myzipx.zip";

$dest_dir = "./dest";

if (!is_dir($dest_dir)) {

    if (!mkdir($dest_dir, 0755, true))

        die("failed to make directory $dest_dir\n");

}

$zip = new ZipArchive;

if (!$zip->open($arch_filename))

    die("failed to open $arch_filename");


for ($i = 0; $i < $zip->numFiles; ++$i) {

    $path = $zip->getNameIndex($i);

    $ext = pathinfo($path, PATHINFO_EXTENSION);

    if (!preg_match('/(?:pdf)/i', $ext))

        continue;

    $dest_basename = pathinfo($path, PATHINFO_BASENAME);

    echo $path, PHP_EOL;


    copy("$path", "$dest_dir/{$dest_basename}");

}


$zip->close();

?>

发生了一件奇怪的事情,因为这段代码只工作了 15 分钟,现在抛出警告


(!)警告:复制(myzipx/x/x.pdf):无法打开流:第 21 行的 C:\wamp64\www\zip_ex\x\zip_img.php 中没有这样的文件或目录


但该文件存在并回显正确的文件名。不明白似乎是什么问题..任何帮助表示赞赏。


白板的微信
浏览 151回答 2
2回答

MMMHUHU

你的尝试copy()是正确的。与ZipArchive::extractTo()(提取并在目标中创建子文件夹)不同,该方法copy()只是将指定文件从存档复制/提取到目标。这个例子应该工作:$archive = "testarchive.zip";$dest_dir = "./dest";if (!is_dir($dest_dir)) {&nbsp; &nbsp; if (!mkdir($dest_dir, 0755, true)) die("failed to make directory $dest_dir\n");}$zip = new ZipArchive;if (!$zip->open($archive)) die("failed to open $archive");for($i = 0; $i < $zip->numFiles; $i++) {&nbsp; &nbsp; $file_name = $zip->getNameIndex($i);&nbsp; &nbsp; $file_info = pathinfo($file_name);&nbsp; &nbsp; $file_ext = pathinfo($file_name, PATHINFO_EXTENSION);&nbsp; &nbsp; if (preg_match('/pdf/i', $file_ext)) {&nbsp; &nbsp; &nbsp; &nbsp; copy("zip://".$archive."#".$file_name, $dest_dir.'/'.$file_info['basename']);&nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$zip->close();测试档案结构:xxxxx@xxxxxx:~/Documents$ tree testarchivetestarchive└── test&nbsp; &nbsp; └── blubb&nbsp; &nbsp; &nbsp; &nbsp; └── test.pdf然后将该文件夹testarchive压缩为testarchive.zip.运行上面的代码后:xxxxx@xxxxxx:~/Documents$ tree destdest└── test.pdf

尚方宝剑之说

您需要从 zip 存档中提取。for ($i = 0; $i < $zip->numFiles; ++$i) {&nbsp; &nbsp; $path = $zip->getNameIndex($i);&nbsp; &nbsp; $ext = pathinfo($path, PATHINFO_EXTENSION);&nbsp; &nbsp; if (preg_match('/pdf/i', $ext)) {&nbsp; &nbsp; &nbsp; &nbsp; $dest_basename = pathinfo($path, PATHINFO_BASENAME);&nbsp; &nbsp; &nbsp; &nbsp; echo $path, PHP_EOL;&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents("$dest/$dest_basename", $zip->getFromIndex($i))&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答