PHP 下载文件
PHP 下载文件
PHP 使您可以使用内置的 readfile() 函数轻松下载文件。 readfile() 函数读取文件并将其写入输出缓冲区。
PHP readfile() 函数
语法
int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )
$filename: 代表文件名
$use_include_path: 可选参数。默认情况下为 false。您可以将其设置为 true 以在包含的路径中搜索文件。
$context: 表示上下文流资源。
int: 返回从文件中读取的字节数。
PHP 下载文件示例: 文本文件
文件: download1.php
<?php
$file_url = 'http://www.lidihuo.com/f.txt';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: utf-8");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
?>
PHP 下载文件示例: 二进制文件
文件: download2.php
<?php
$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
?>