如何解決php壓縮檔案失敗的問題

2020-10-06 15:00:36

php壓縮檔案失敗是因為缺少相關程式碼語句,其解決辦法就是在程式碼檔案中新增「ob_clean();flush();」語句即可。

推薦:《》

PHP實現檔案下載功能,提示壓縮包損壞及打不開的解決方法

// 檔案下載
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;
}

其實大多數的錯誤都是在缺少

ob_clean();
flush();

以上就是如何解決php壓縮檔案失敗的問題的詳細內容,更多請關注TW511.COM其它相關文章!