欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

php一点按钮就下载功能源码,php实现强制文件下载方法的源码参考

发布时间:2023/12/20 php 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 php一点按钮就下载功能源码,php实现强制文件下载方法的源码参考 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

有时我们希望如图片、文本文档、网页、mp3、pdf等内容,当点击对应链接时直接下载,而不是在网页上显示,那么就需要强制设置header头信息。以下为一段不会产生乱码的php函数实现代码,其他程序语言也可参考之编写实现

/**

* 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一点按钮就下载功能源码,php实现强制文件下载方法的源码参考的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。