Php Symfony无法打开下载的zip文件
我想下载一个zip文件。所以我试着让代码如下:Php Symfony无法打开下载的zip文件,php,symfony,download,Php,Symfony,Download,我想下载一个zip文件。所以我试着让代码如下: $zip = new ZipArchive(); $create = $zip->open($zipName, ZipArchive::CREATE); if ($create === TRUE) { // check if the zip file is created $basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
$basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
foreach ($listToken as $token) {
$file = $repoMarks->findByToken($token);
if($file) {
$fileName = $file[0]->getNameOnServer();
$filePath = $basePath . $fileName;
$root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
$filePath = $root . '/' . $fileName;
if (file_exists($filePath)) {
$zip->addFile($filePath, $fileName);
}
}
}
$zip->close();
$root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
$zipFilePath = $root . '/' . $zipName;
// prepare BinaryFileResponse
$response = new BinaryFileResponse($zipFilePath);
$response->trustXSendfileTypeHeader();
$response->headers->set('Cache-Control', 'public');
$response->headers->set('Content-type', 'application/zip');
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
$zipName,
iconv('UTF-8', 'ASCII//TRANSLIT', $zipName)
);
return $response;
}
我认为这是成功的。但是,当我试图打开zip文件时,出现了如下错误加载存档文件时出错。
然后我试着把代码写成这样
$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
$basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
foreach ($listToken as $token) {
$file = $repoMarks->findByToken($token);
if($file) {
$fileName = $file[0]->getNameOnServer();
$filePath = $basePath . $fileName;
$root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
$filePath = $root . '/' . $fileName;
if (file_exists($filePath)) {
$zip->addFile($filePath, $fileName);
}
}
}
$zip->close();
header('Content-Type', 'application/zip');
header('Content-disposition: attachment; filename="' . $zipName . '"');
header('Content-Length: ' . filesize($zipName));
readfile($zipName);
}
但我什么都没有。当我将其更改为以下内容时,同样的情况也会发生:
$zip = new ZipArchive();
$create = $zip->open($zipName, ZipArchive::CREATE);
if ($create === TRUE) { // check if the zip file is created
$basePath = $this->container->getParameter('kernel.root_dir').'/../marks/';
foreach ($listToken as $token) {
$file = $repoMarks->findByToken($token);
if($file) {
$fileName = $file[0]->getNameOnServer();
$filePath = $basePath . $fileName;
$root = realpath($this->container->getParameter('kernel.root_dir') . '/../marks');
$filePath = $root . '/' . $fileName;
if (file_exists($filePath)) {
$zip->addFile($filePath, $fileName);
}
}
}
$zip->close();
header("HTTP/1.1 303"); // 303 is technically correct for this type of redirect
header("Location: http://{$_SERVER['HTTP_HOST']}/" . $fileName);
}
是否有人可以帮助我解决此下载zip文件问题?
加载存档文件时出错。
在客户端发生的原因是:
zip
文件的应用程序zip
文件损坏或缺少扩展名sudo apt get update
这是客户端(连接或程序错误)或文件本身的问题。请尝试使用另一台电脑打开该文件。您有类似于此的问题,您仍然可以在此处打开该问题,嗯,没有。。那个问题是关于找不到的班级的。。但在这个问题上,拉链已经没问题了。。但是在
$zip->close()
之后,我想将zip解析为客户端下载的响应。第一个代码(使用binaryFileResponse)是可以的,但是当我尝试打开压缩文件时,它无法在客户端打开,而在服务器上则可以。谢谢你的回答!我只是编辑你的评论并添加一个列表,第4点。当我禁用flareget时,我的第一个代码可以正常工作。我明白了,很高兴它有帮助:)