0

我使用以下代码压缩在线图像并将压缩文件下载到本地。但显示“ 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";
}

我该如何解决?谢谢

4

1 回答 1

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

于 2016-02-27T05:54:58.963 回答