php - 文件存在但收到无法打开流的警告:没有这样的文件或目录
问题描述
<?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 中没有这样的文件或目录
但该文件存在并回显正确的文件名。不明白似乎是什么问题..任何帮助表示赞赏。
解决方案
你的尝试copy()
是正确的。与ZipArchive::extractTo()
(提取并在目标中创建子文件夹)不同,该方法copy()
只是将指定文件从存档复制/提取到目标。
这个例子应该工作:
$archive = "testarchive.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($archive)) die("failed to open $archive");
for($i = 0; $i < $zip->numFiles; $i++) {
$file_name = $zip->getNameIndex($i);
$file_info = pathinfo($file_name);
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
if (preg_match('/pdf/i', $file_ext)) {
copy("zip://".$archive."#".$file_name, $dest_dir.'/'.$file_info['basename']);
}
}
$zip->close();
测试档案结构:
xxxxx@xxxxxx:~/Documents$ tree testarchive
testarchive
└── test
└── blubb
└── test.pdf
然后将该文件夹testarchive
压缩为testarchive.zip
.
运行上面的代码后:
xxxxx@xxxxxx:~/Documents$ tree dest
dest
└── test.pdf
推荐阅读
- android - 在应用程序更新检查不起作用,即使用户更新应用程序(即安装的最新版本)
- laravel - Laravel:尝试将特定订单返回给用户时出错
- java - 使用 2D 和 1D 数组计算利率
- excel - 在变量表 Excel 中搜索数据
- python - 如何在 PYSPARK_SUBMIT_ARGS 中添加多个环境
- c - 如何知道当前的掩码
- python - 使用 Scrapy Python 在每两行之后创建空白行
- python - Can anyone help me identify that heroku application error
- c# - C# .Net Core 应用程序:来自多个线程的 ReadKey
- sql - Datepart() 函数未转换为 bigint