当前位置:首页 > PHP

php 实现下载

2026-04-02 21:08:43PHP

实现 PHP 文件下载功能

PHP 可以通过设置 HTTP 头信息实现文件下载功能。以下是一个基本的实现方法:

<?php
$file_path = 'path/to/your/file.ext'; // 文件路径
$file_name = 'download_name.ext'; // 下载时显示的文件名

if (file_exists($file_path)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $file_name . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file_path));
    readfile($file_path);
    exit;
} else {
    echo '文件不存在';
}
?>

处理大文件下载

对于大文件下载,可以使用分块读取的方式避免内存问题:

<?php
$file_path = 'large_file.zip';
$chunk_size = 1024 * 1024; // 1MB chunks

if (file_exists($file_path)) {
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
    header('Content-Length: ' . filesize($file_path));

    $handle = fopen($file_path, 'rb');
    while (!feof($handle)) {
        echo fread($handle, $chunk_size);
        ob_flush();
        flush();
    }
    fclose($handle);
    exit;
}
?>

安全下载注意事项

确保下载文件的安全性,避免目录遍历攻击:

<?php
$allowed_dir = '/safe/download/directory/';
$requested_file = $_GET['file'];

// 验证文件路径是否在允许目录中
$real_path = realpath($allowed_dir . $requested_file);
if (strpos($real_path, $allowed_dir) !== 0) {
    die('非法文件请求');
}

if (file_exists($real_path)) {
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($real_path) . '"');
    readfile($real_path);
    exit;
}
?>

强制下载而非打开

某些文件类型可能会被浏览器直接打开而非下载,可以强制下载:

<?php
$file = 'document.pdf';
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile($file);
?>

进度显示

对于超大文件,可以添加下载进度显示:

php 实现下载

<?php
$file = 'huge_file.iso';
$size = filesize($file);
header('Content-Length: ' . $size);

$handle = fopen($file, 'rb');
while (!feof($handle)) {
    echo fread($handle, 8192);
    ob_flush();
    flush();

    // 这里可以添加进度计算逻辑
    $sent = ftell($handle);
    $percent = ($sent / $size) * 100;
    // 记录或显示进度
}
fclose($handle);
?>

这些方法提供了从基本到高级的PHP文件下载实现方案,可以根据实际需求选择适合的方式。

标签: php
分享给朋友:

相关文章

php实现分页

php实现分页

PHP实现分页的基本方法 使用LIMIT和OFFSET进行数据库查询分页。通过计算当前页码和每页显示数量,动态生成SQL语句。 $page = isset($_GET['page']) ? (int…

php 路由实现

php 路由实现

PHP 路由实现方法 在 PHP 中实现路由功能有多种方式,以下是几种常见的实现方法: 使用原生 PHP 实现 通过解析 URL 并匹配对应的控制器和动作: $request = $_SERVER…

php 实现验证码

php 实现验证码

使用 PHP 生成验证码 验证码(CAPTCHA)用于防止自动化脚本提交表单,以下是几种常见的 PHP 实现方法。 使用 GD 库生成图片验证码 GD 库是 PHP 的图像处理扩展,可用于生成验证码…

php验证码的实现

php验证码的实现

PHP 验证码实现方法 使用 GD 库生成基础验证码 创建画布并生成随机字符串作为验证码内容: session_start(); $width = 120; $height = 40; $image…

基于php的设计与实现

基于php的设计与实现

PHP 设计与实现的关键要素 环境搭建与框架选择 PHP开发需配置服务器环境(如Apache/Nginx)、PHP版本(建议7.4+)及数据库(MySQL/MariaDB)。主流框架包括Laravel…

php 实现文件下载

php 实现文件下载

实现文件下载的基本方法 使用 PHP 实现文件下载的核心是通过设置 HTTP 头部信息,强制浏览器将文件作为附件下载而非直接显示。以下是一个基础实现示例: $file_path = '/path/t…