|
本文介绍下,用php实现文件下载的一些代码,有需要的朋友不妨参考下。
1,php下载函数 借助header()函数与readfile()函数实现文件下载功能。
<?php function DownloadFile($filename)
{
// Check filename
if (empty($filename) || !file_exists($filename))
{
return FALSE;
}
// Create download file name to be displayed to user
$saveasname = basename($filename);
// Send binary filetype HTTP header
header('Content-Type: application/octet-stream');
// Send content-length HTTP header
header('Content-Length: '.filesize($filename));
// Send content-disposition with save file name HTTP header
header('Content-Disposition: attachment; filename="'.$saveasname.'"');
// Output file
readfile($filename);
// Done
return TRUE;
}
?>
2,php文件下载脚本 更完善的php下载的自定义函数。
<?php //////////////////////////////////////////////////////////////////////
// DOWNLOAD.PHP -- Download Utility
// Get download file id (assume it's passed as a form or URL parameter)
$id = getGlobalVar('id', 1);
// Check download file id parameter, get download file name, download file
// (assuming (global) variable $id specifies download file id)
if (empty($id) || !DownloadFile(getDownloadFileName($id)))
{
die("No Download!");
}
// Only functions beyond this point
function getDownloadFilename($fileid)
// Get download file pathname
// Returns: download file pathname
// Parameters:
// $fileid : File identifier
{
// Download file list
$DLFILES = array(
'TOOL1' => 'download/tool1_v30.exe',
'PROG2' => 'download/prog2setup.exe',
);
// Get/check download file name
if (empty($fileid) || empty($DLFILES[$fileid]))
{
return '';
}
// Set base directory to document root directory
// (could also be set to a directory outside document root!)
$basedir = getGlobalVar('DOCUMENT_ROOT');
// Build and return download file name
return $basedir.'/'.$DLFILES[$fileid];
}
function DownloadFile($filename)
// Download file
// Returns: TRUE if download successfully started, FALSE if download failed
// Parameters:
// $filename : Download file pathname
{
// Verify filename
if (empty($filename) || !file_exists($filename))
{
return FALSE;
}
// Create download file name to be displayed to user
$saveasname = basename($filename);
// Send binary filetype HTTP header
header('Content-Type: application/octet-stream');
// Send content-length HTTP header
header('Content-Length: '.filesize($filename));
// Send content-disposition with save file name HTTP header
header('Content-Disposition: attachment; filename="'.$saveasname.'"');
// Output file
readfile($filename);
// Download successfully started
return TRUE;
}
function getGlobalVar($g, $formflag = 0)
// Get global PHP variable value
// Returns: global variable value or empty string if not available
// Parameters:
// $g : Global PHP variable name
// $formflag : Flag - global var from GET/POST input
{
if (empty($g))
{
return 0;
}
// Try superglobal access (PHP 4.1.0+)
if ($formflag)
{
if (isset($_POST[$g]))
{
return $_POST[$g];
}
if (isset($_GET[$g]))
{
return $_GET[$g];
}
if (isset($_REQUEST[$g]))
{
return $_REQUEST[$g];
}
}
else
{
if (isset($_SERVER[$g]))
{
return $_SERVER[$g];
}
if (isset($_ENV[$g]))
{
return $_ENV[$g];
}
}
// Try superglobal access (PHP 3.0.0+)
if (isset($GLOBALS[$g]))
{
return $GLOBALS[$g];
}
// Try global variable access (PHP 3+)
global $$g;
if (!empty($$g))
{
return $$g;
}
// Assume global variable empty/not set
return '';
}
?>
将以上脚本保存为dl.php,在应用时传入id参数即可。 例如: Download Program 2 也可以通过php的重定向语句Location来实现,例如:
<?php header("Location: http://www.YourDomain.com/dl.php?id=PROG2");
exit;
?>
以上的代码,可以防止用户直接访问下载文件,起到了一定的文件保护,甚至是防盗链的功能。 以下的代码,可以依据http头信息,作些介绍,从而提供更安全的文件下载。
ERMEB域名PHP离线网络授权系统
下载
感谢您选择使用ERMEB域名授权离线网络验证系统(简称:ERMEB域名授权系统)是ERMEB团队开发,ERMEB域名授权系统是国内最稳定,最强大,最先进的域名授权管理平台解决方案之一,ERMEB域名授权系统采用PHP +Mysql的技术,ERMEB域名授权系统框架使用Thinkphp6/mysql数据库基于Markdown开发者文档开发而成,项目安装请参考ThinkPHP官方文档及下面的服务环境说 代码:
<?php function DownloadFile($filename)
{
// Check filename
if (empty($filename) || !file_exists($filename))
{
return FALSE;
}
// Create download file name to be displayed to user
$saveasname = basename($filename);
// Send binary filetype HTTP header
header('Content-Type: application/octet-stream');
// Send content-length HTTP header
header('Content-Length: '.filesize($filename));
// Send content-disposition with save file name HTTP header
// (using workaround for MSIE 5.5 SP1 / MSIE 6.01 bugs/problems)
$browser = getGlobalVar('HTTP_USER_AGENT');
if (strstr('MSIE 5.5', $browser)
|| strstr('MSIE 6.01', $browser))
{
header('Content-Disposition: filename="'.$saveasname.'"');
}
else
{
header('Content-Disposition: attachment; filename="'.$saveasname.'"');
}
// Send Content-Transfer-Encoding HTTP header
// (use binary to prevent files from being encoded/messed up during transfer)
header('Content-Transfer-Encoding: binary');
// Output file
readfile($filename);
// Done
return TRUE;
}
function getGlobalVar($g, $formflag = 0)
// Get global PHP variable value
// Returns: global variable value or empty string if not available
// Parameters:
// $g : Global PHP variable name
// $formflag : Flag - global var from GET/POST input
{
if (empty($g))
{
return 0;
}
// Try superglobal access (PHP 4.1.0+)
if ($formflag)
{
if (isset($_GET[$g]))
{
return $_GET[$g];
}
if (isset($_POST[$g]))
{
return $_POST[$g];
}
if (isset($_REQUEST[$g]))
{
return $_REQUEST[$g];
}
}
else
{
if (isset($_SERVER[$g]))
{
return $_SERVER[$g];
}
if (isset($_ENV[$g]))
{
return $_ENV[$g];
}
}
// Try superglobal access (PHP 3.0.0+)
if (isset($GLOBALS[$g]))
{
return $GLOBALS[$g];
}
// Try global variable access (PHP 3+)
global $$g;
if (!empty($$g))
{
return $$g;
}
// Assume global variable empty/not set
return '';
}
?>
<?php function DownloadFile($filename)
{
// Check filename
if (empty($filename) || !file_exists($filename))
{
return FALSE;
}
// Create download file name to be displayed to user
$saveasname = basename($filename);
// Send binary filetype HTTP header
header('Content-Type: application/octet-stream');
// Send content-length HTTP header
header('Content-Length: '.filesize($filename));
// Send content-disposition with save file name HTTP header
// (using workaround for MSIE 5.5 SP1 / MSIE 6.01 bugs/problems)
$browser = getGlobalVar('HTTP_USER_AGENT');
if (strstr('MSIE 5.5', $browser)
|| strstr('MSIE 6.01', $browser))
{
header('Content-Disposition: filename="'.$saveasname.'"');
}
else
{
header('Content-Disposition: attachment; filename="'.$saveasname.'"');
}
// Send Content-Transfer-Encoding HTTP header
// (use binary to prevent files from being encoded/messed up during transfer)
header('Content-Transfer-Encoding: binary');
// Output file
readfile($filename);
// Done
return TRUE;
}
function getGlobalVar($g, $formflag = 0)
// Get global PHP variable value
// Returns: global variable value or empty string if not available
// Parameters:
// $g : Global PHP variable name
// $formflag : Flag - global var from GET/POST input
{
if (empty($g))
{
return 0;
}
// Try superglobal access (PHP 4.1.0+)
if ($formflag)
{
if (isset($_GET[$g]))
{
return $_GET[$g];
}
if (isset($_POST[$g]))
{
return $_POST[$g];
}
if (isset($_REQUEST[$g]))
{
return $_REQUEST[$g];
}
}
else
{
if (isset($_SERVER[$g]))
{
return $_SERVER[$g];
}
if (isset($_ENV[$g]))
{
return $_ENV[$g];
}
}
// Try superglobal access (PHP 3.0.0+)
if (isset($GLOBALS[$g]))
{
return $GLOBALS[$g];
}
// Try global variable access (PHP 3+)
global $$g;
if (!empty($$g))
{
return $$g;
}
// Assume global variable empty/not set
return '';
}
?> |
0
0
相关文章
如何在命令行中将 PHP 内置服务器绑定到 example.com 域名
如何将 PHP 内置服务器绑定到自定义域名(如 example.com)
PHP怎么设置文件夹缓存_PHP启用文件夹缓存优化【技巧】
jQuery .load() 方法加载外部 PHP 页面内容的完整实践指南
如何批量下载文件、重命名并合并为单个文本文件
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。
616
2026.02.13
本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。
194
2026.02.13
本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。
91
2026.02.13
本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。
20
2026.02.13
本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。
54
2026.02.13
本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。
15
2026.02.12
本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。
598
2026.02.12
本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。
56
2026.02.12
热门下载
相关下载
精品课程
最新文章



