PHPはファイルダウンロード機能を実現し、圧縮パッケージの破損と開けられない解決方法を提示する.
2306 ワード
//
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();