Home >Backend Development >PHP Problem >How to solve the problem of PHP compressed file failure

How to solve the problem of PHP compressed file failure

藏色散人
藏色散人Original
2020-10-06 13:28:463301browse

The PHP compressed file failed because of the lack of relevant code statements. The solution is to add the "ob_clean();flush();" statement to the code file.

How to solve the problem of PHP compressed file failure

Recommended: "PHP Video Tutorial"

PHP implements the file download function, prompting that the compressed package is damaged and cannot be opened Open solution

// 文件下载
public static function downFile($id,$admin_id,$cid,$type)
{
$company = checkAdminCompany($admin_id,$cid);
        if (!$company) {
            return [
                'status' => -4,
                'statusMsg' => '公司不存在'
            ];
        }
        if ($type=='file') {
        // 判断是否有权限下载文件
$checkFileRole = self::checkFileRole($admin_id,$cid,$id);
if (!$checkFileRole) {
return [
'status' => 777,
'statusMsg' => '没有操作权限'
];
}
        $select_sql = "SELECT * FROM cloud_storage WHERE id in ($id)";
send_execute_sql($select_sql,$fileInfo);
        } elseif ($type == 'list') {
        // 判断是否有权限移动目录下的文件
$checkRole = self::checkRole($admin_id,$cid,$id);
if (!$checkRole) {
return [
'status' => '777',
'statusMsg' => '没有操作权限'
];
}
        $select_sql = "SELECT * FROM cloud_storage WHERE list_id = $id";
send_execute_sql($select_sql,$fileInfo);
        }
if (empty($fileInfo)) {
        return [
'status' => -400,
'statusMsg' => '文件不存在'
];
        }
 
        foreach ($fileInfo as $key => $value) {
$paths[$key] = $fileInfo[$key]['path'];
}
        //这里需要注意该目录是否存在,并且有创建的权限
$filename = 'logs/down/test.zip'; 
if(!file_exists($filename)){
$zip = new ZipArchive();
if ($zip->open($filename, ZipArchive::CREATE)==TRUE) {
foreach( $paths as $val){
  if(file_exists($val)){
   $zip->addFile( $val, basename($val));
  }
 }
 $zip->close();
}
}
if(!file_exists($filename)){
exit("无法找到文件");
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小
ob_clean();
flush();
@readfile($filename);
unlink($fileurl);
   exit;
}

In fact, most of the errors are due to the lack of

ob_clean();
flush();

The above is the detailed content of How to solve the problem of PHP compressed file failure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn