Adi*_*ael 55
假设它在服务器上:
readfile() - 输出文件
来自http://php.net/manual/en/function.readfile.php的示例
Ray*_*jax 38
以下是使用PHP发送文件所需的内容:
$filename = "whatever.jpg";
if(file_exists($filename)){
//Get file type and set it as Content Type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('Content-Type: ' . finfo_file($finfo, $filename));
finfo_close($finfo);
//Use Content-Disposition: attachment to specify the filename
header('Content-Disposition: attachment; filename='.basename($filename));
//No cache
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//Define file size
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
exit;
}
Run Code Online (Sandbox Code Playgroud)
正如Julian Reschke评论的那样,经过验证的答案可能会起作用,但它充满了无用的标题.内容类型应设置为文件的实际类型,或者某些浏览器(尤其是移动浏览器)可能无法正确下载.
如果您使用的是 Apache 或 Lighty,那么从性能的角度来看,执行此操作的“最佳”方法是使用X-Sendfile标头。请参阅本教程:https : //www.h3xed.com/programming/how-to-use-x-sendfile-with-php-apache
| 归档时间: |
|
| 查看次数: |
76763 次 |
| 最近记录: |