PHP基础 专题
您的位置:php > PHP基础 专题 > PHP下载文件
PHP下载文件
作者:--    发布时间:2019-11-20

在php中,可使用内置的readfile()函数来实现文件下载。 readfile()函数读取一个文件并将其写入输出缓冲区。

php readfile()函数

语法

int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )
  • $filename:表示文件名
  • $use_include_path:它是可选参数。它默认为false。可以将其设置为true以搜索included_path中的文件。
  • $context:表示上下文流资源。
  • int:它返回从文件读取的字节数。

hp下载文件示例:文本文件

在您的网站目录下(我使用的是 d:/wamp/www)创建一个文本文件: text.txt

文件: download1.php

<?php  
$file_url = 'http://localhost/text.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);  
?>

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册