我想要一个 php 脚本来下载任何类型的文件而不打开它。我找到了以下功能,在我看来有点不同,但我不知道哪个更好。请告诉我哪个更好更可靠:
来自PHP 手册
$file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; }
来自其他教程:
header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=\"{$file->filename}\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . $filesize); // download // @readfile($file_path); $dwn_file = @fopen($file_path,"rb"); if ($dwn_file) { while(!feof($dwn_file)) { print(fread($dwn_file, 1024*8)); flush(); if (connection_status()!=0) { @fclose($dwn_file); die(); } } @fclose($dwn_file); }