在zf2中下载后删除zip文件

时间:2016-09-26 05:26:40

标签: php download zend-framework2 zip

我正在尝试在下载后删除zip文件。我尝试以下代码。但它显示错误

$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($zipname, 'r'));
$response->setStatusCode(200);
$headers = new \Zend\Http\Headers();
$headers->addHeaderLine('Content-Type', 'application/octet-stream')
        ->addHeaderLine('Content-Disposition', 'attachment; filename='.basename($zipname))
        ->addHeaderLine('Content-Length', filesize($zipname));

$response->setHeaders($headers);
unlink($zipname);
return $response;

下载后,当我在记事本中打开下载的文件时,显示如下:

  

取消关联(D:\ xampp \ htdocs \ medgenom \ development \ uploads / Bulk_formE_Download_0.91481200 1474869559.zip):权限被拒绝

1 个答案:

答案 0 :(得分:0)

我使用以下代码解决了

header('Content-Type', 'application/octet-stream');
header("Content-Disposition: attachment; filename=".basename($zipname));
header('Content-Length', filesize($zipname));
header("Pragma: no-cache");
header("Expires: 0");
readfile($zipname);

unlink($zipname);