时时商务社区
标题:
php强制文件下载而非在浏览器打开的自定义函数分享
[打印本页]
作者:
qz234
时间:
2018-2-14 05:58
有时我们希望如图片、文本文档、网页、mp3、pdf等内容,当点击对应链接时直接下载,而不是在网页上显示,那么就需要强制设置header头信息。以下为一段不会产生乱码的php函数实现代码,其他程序语言也可参考之编写实现。
[U]复制代码[/U] 代码如下:
/**
* Downloader
*
* @param $archivo
* path al archivo
* @param $downloadfilename
* (null|string) el nombre que queres usar para el archivo que se va a descargar.
* (si no lo especificas usa el nombre actual del archivo)
*
* @return file stream
*/
function download_file($archivo, $downloadfilename = null) {
if (file_exists($archivo)) {
$downloadfilename = $downloadfilename !== null ? $downloadfilename : basename($archivo);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $downloadfilename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($archivo));
ob_clean();
flush();
readfile($archivo);
exit;
}
}
您可能感兴趣的文章:
用php实现的下载css文件中的图片的代码
php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
抓取并下载CSS中所有图片文件的php代码
使用PHP下载CSS文件中的图片的代码
php实现图片文件与下载文件防盗链的方法
php实现的css文件背景图片下载器代码
php获取CSS文件中图片地址并下载到本地的方法
使用PHP实现下载CSS文件中的图片
php强制下载文件函数
php简单实现文件或图片强制下载的方法
欢迎光临 时时商务社区 (http://bbs.4435.cn/)
Powered by Discuz! X3.2