我使用以下代码压缩在线图像并将zip文件下载到本地。但是显示" file not exist
"。
$url = "http://www.google.com/intl/en_ALL/images/logo.gif";
$zip = new ZipArchive;
$zipname = sys_get_temp_dir() . "/" . time() . ".zip";
if ($zip->open($zipname, ZipArchive::CREATE) === TRUE) {
$zip->addFromString('logo.gif', file_get_contents($url));
$zip->close();
if (file_exists($zipname)) {
// force to download the zip
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $zipname . '"');
readfile($zipname);
// remove zip file from temp path
unlink($zipname);
echo "ok";
} else {
echo "file not exist";
}
} else {
echo "failed";
}
我该如何解决?感谢
答案 0 :(得分:0)
您需要使用某个可写目录的绝对路径来定义$zipname
。例如,您可以使用sys_get_temp_dir()
函数来获取php temp dir路径。
$zipname = sys_get_temp_dir() . "/" . time() . ".zip";
点击此处了解有关sys_get_temp_dir()
功能的更多信息:http://php.net/manual/en/function.sys-get-temp-dir.php