【发布时间】:2017-11-16 15:31:35
【问题描述】:
我收到以下警告
PHP Warning: ZipArchive::extractTo(/mnt/c/some/folder\data.json):
failed to open stream: Invalid argument in /mnt/c/somefile.php on line 54
使用此代码,在运行 PHP 7.1 的 Windows 上使用 Ubuntu 子系统提取任何 zip 文件:
<?php
class someClass
{
public static function unzip($fn, $to = null)
{
$zip = new ZipArchive;
if (is_null($to)) {
$to = self::dirname($fn) . DIRECTORY_SEPARATOR . self::filename($fn);
}
if (!is_dir($to)) {
self::mkdir($to, 0755, true);
}
$res = $zip->open($fn);
if ($res === true) {
$zip->extractTo($to);
$zip->close();
return $to;
} else {
return false;
}
}
}
?>
相同的代码在 Windows 下的 PHP 7.1 和 Linux (CentOS) 下的 PHP 7.1 上运行良好。
【问题讨论】:
-
只是一个愚蠢的观点:你设置了
$ds = DIRECTORY_SEPARATOR;然后从不使用$ds但你确实使用DIRECTORY_SEPARATOR??? -
哦,是的,在我的原始代码中我有更多的东西,我去掉了大部分不相关的东西,但我一定把它留在了!编辑了我的答案并删除了。
标签: php linux windows ubuntu zip