当前位置:首页 > PHP

php怎么实现下载

2026-04-03 05:15:29PHP

实现文件下载的基本方法

使用PHP的header()函数设置HTTP头信息,结合readfile()函数输出文件内容。以下是一个简单示例:

$file_path = '/path/to/your/file.pdf';
$file_name = 'download.pdf';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;

处理大文件下载

对于大文件,使用分块读取避免内存问题:

$chunk_size = 1024 * 1024; // 1MB chunks
$handle = fopen($file_path, 'rb');
while (!feof($handle)) {
    echo fread($handle, $chunk_size);
    ob_flush();
    flush();
}
fclose($handle);

安全下载控制

验证用户权限和文件路径,防止目录遍历攻击:

$base_dir = '/safe/directory/';
$requested_file = $_GET['file'];
$real_path = realpath($base_dir . $requested_file);

if (strpos($real_path, $base_dir) !== 0 || !file_exists($real_path)) {
    die('Invalid file');
}

断点续传支持

实现HTTP Range请求支持:

$size = filesize($file_path);
$start = 0;
$end = $size - 1;

if (isset($_SERVER['HTTP_RANGE'])) {
    if (preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches)) {
        $start = intval($matches[1]);
        if (!empty($matches[2])) {
            $end = intval($matches[2]);
        }
    }
    header('HTTP/1.1 206 Partial Content');
    header("Content-Range: bytes $start-$end/$size");
}

header("Accept-Ranges: bytes");
header("Content-Length: " . ($end - $start + 1));

强制下载特定MIME类型

设置特定的Content-Type强制浏览器下载:

$mime_types = [
    'pdf' => 'application/pdf',
    'zip' => 'application/zip',
    // 添加更多MIME类型
];

$ext = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
if (array_key_exists($ext, $mime_types)) {
    header('Content-Type: ' . $mime_types[$ext]);
}

下载进度监控

使用输出缓冲和回调函数监控下载进度:

php怎么实现下载

function readfile_chunked($filename, $chunk_size = 1024, $callback = null) {
    $handle = fopen($filename, 'rb');
    $total = filesize($filename);
    $sent = 0;

    while (!feof($handle)) {
        $buffer = fread($handle, $chunk_size);
        echo $buffer;
        $sent += strlen($buffer);
        if (is_callable($callback)) {
            call_user_func($callback, $sent, $total);
        }
        ob_flush();
        flush();
    }
    fclose($handle);
}

标签: php
分享给朋友:

相关文章

php实现验证码

php实现验证码

PHP实现验证码的方法 使用GD库生成验证码 GD库是PHP中处理图像的扩展,可用于生成验证码图片。 <?php session_start(); $width = 120; $height…

git php实现

git php实现

Git 与 PHP 的集成实现 使用 PHP 执行 Git 命令 通过 PHP 的 exec()、shell_exec() 或 passthru() 函数可以直接调用 Git 命令,适用于服务器端操作…

php实现排序

php实现排序

PHP 数组排序方法 PHP 提供了多种内置函数用于数组排序,适用于不同的排序需求。以下是常见的排序方法: 按值升序排序 sort() 函数对数组按值升序排序,重置键名(索引数组适用): $num…

php 实现验证码

php 实现验证码

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

php实现文件的下载

php实现文件的下载

PHP 实现文件下载的方法 在 PHP 中实现文件下载功能通常需要设置正确的 HTTP 头信息,并输出文件内容。以下是几种常见的实现方式: 使用 header() 函数强制下载 通过设置 Conte…

php 伪静态实现

php 伪静态实现

PHP 伪静态实现方法 伪静态(URL Rewrite)是一种通过服务器配置将动态URL转换为静态URL的技术,提升URL的可读性和SEO效果。以下是几种常见的实现方式: 使用Apache的mod_…