• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 实现下载

php 搞代码 3年前 (2022-01-23) 27次浏览 已收录 0个评论

/*
本文章提供的三款文件下载代码有二款是支持本地服务器文件下载的,另一款支持下载远程服务器上的文件下载到本地哦。
*/

function download($file_dir,$file_name)
//参数说明:
//file_dir:文件所在目录
//file_name:文件名
{
$file_dir = chop($file_dir);//去掉路径中多余的空格
//得出要下载的文件的路径
if($file_dir != ”)
{
$file_path = $file_dir;
if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != ‘/’)
$file_path .= ‘/’;
$file_path .= $file_name;
}
else
$file_path = $file_name;

//判断要下载的文件是否存在
if(!file_exists($file_path))
{
echo ‘对不起,你要下载的文件不存在。’;
return false;
}

$file_size = filesize($file_path);

header(“content-type: application/octet-stream”);
header(“accept-ranges: bytes”);
header(“accept-length: $file_size”);
header(“content-disposition: attachment; filename=”.$file_name);

$fp = fopen($file_path,”r”);
$buffer_size = 1024;
$cur_pos = 0;

while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
{
$buffer = fread($fp,$buffer_size);
echo $buffer;
$cur_pos += $buffer_size;
}

$buffer = fread($fp,$file_size-$cur_pos);
echo $buffer;
fclose($fp);
return true;

}

?>
<?php

$file_name = “info_check.exe”;
$file_dir = “/public/www/download/”;
if (!file_exists($file_dir . $file_name)) { //检查文件是否存在
echo “文件找不到”;
exit;
} else {
$file = fopen($file_dir . $file_name,”r”); // 打开文件
// 输入文件标签
header(“content-type: application/octet-stream”);
header(“accept-ranges: bytes”);
header(“accept-length: “.filesize($file_dir . $file_name));
header(“content-disposition: attachment; filename=” . $file_name);
// 输出文件内容
echo fread($file,filesize($file_dir . $file_name));
fclose($file);
exit;}
?>

<?
// php 文件下载代码:php如何实现文件下载功能(支持远程文件下载)

//如果文件路径是http和ftp,下载代码如下:

$file_name = “info_check.exe”;
$file_dir = ” http://www.zzarea.com/&#8221;;
$file = @ fopen($file_dir . $file_name,”r”);
if (!$file) {
echo “文件找不到”;
} else {
header(“content-type: application/octet-stream”);
header(“content-disposition: attachment; filename=” . $file_name);
while (!feof ($file)) {
echo fread($file,50000);
}
fclose ($file);
}

?>

本文来源网页制作教程网www.zzarea.com 原文链接:http://www.zzarea.com/php100/php-1124.html

在调用过程中,会出现

Warning: Cannot modify header information

这个提示。我在网上找了很多种方法只有一种管用。修改php.ini output

5本文来源gao!daima.com搞$代!码#网#

搞代gaodaima码

_suffering = on或者 =4096

后者比前者在下载中速度要快一些。不明白什么原因。

还有ob_start(); ob_end_flush();这个没起作用。

我的写法是。

<?php ob_start(); ?>
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
$filetype = $_REQUEST[‘filetype’];
//echo $filetype;
$path = dirname(__FILE__).”/http://www.cnblogs.com/seq/”;
$durl = $path.$filename;
$filename = ‘phpcms2008_o2abf32efj883c91a.iso’;
$file = @fopen($durl, ‘r’);
header(“Content-Type: application/octet-stream”);

header(“Accept-Ranges: bytes”);

header(“Accept-Length: “.filesize($durl));

header(“Content-Disposition: attachment; filename=”.$filename);

echo fread($file,filesize($durl));
fclose($file);
ob_end_flush();

?>

是不是写错了?


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 实现下载

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址