|
php实现文件强制下载的一段代码,进入指定页面后就弹出文件下载对话框,恰如梦中那突如其来的流星雨,相当地酷类,哈哈。
php 强制文件下载的代码。
<?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)
* site http://bbs.it-home.org
* @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头部文件(header)信息的处理方法,如果你曾留心,就会发现程序员之家为大家收集的有关php强制文件下载的代码,大多采用的是这样的方式。 |
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号