<?php

ob_start();  

// $file_name="cookie.jpg";
$file_name="abc.jpg"; //用以解决中文不能显示出来的问题
$file_name=iconv("utf-8","gb2312",$file_name);
//$file_sub_path=$_SERVER['DOCUMENT_ROOT']."marcofly/phpstudy/down/down/";
$file_sub_path=$_SERVER['DOCUMENT_ROOT'];
$file_path=$file_sub_path.$file_name;
//首先要判断给定的文件存在与否
if(!file_exists($file_path)){
echo "没有该文件文件";
return ;
}
$fp=fopen($file_path,"r");
$file_size=filesize($file_path);
//下载文件需要用到的头
ob_end_clean(); Header("Content-type: application/octet-stream");
//Header("Content-type: image/jpeg");
Header("Accept-Ranges: bytes");
Header("Accept-Length:".$file_size);
//Header("Content-Disposition: attachment; filename=".basename($file_name));
Header("Content-Disposition: attachment; filename=".$file_name);
$buffer=1024;
$file_count=0;
//向浏览器返回数据
while(!feof($fp) && $file_count<$file_size){
$file_con=fread($fp,$buffer);
$file_count+=$buffer;
echo $file_con;
}
@fclose($fp); exit(0); /*
第二种方法
*/
/*
ob_start();
$file_name="123.jpg";
$file_name = iconv("utf-8","gb2312",$file_name);
if (!is_file($file_name)){
echo "url error!";
} else {
$ua = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/", $ua)) {
$encoded_filename = urlencode(basename($file_name));
$encoded_filename = str_replace("+", "%20", $encoded_filename);
$con_dis = 'Content-Disposition: attachment; filename="' . $encoded_filename . '"';
} else if (preg_match("/Firefox/", $ua)) {
$con_dis = 'Content-Disposition: attachment; filename*="utf8\'\'' . basename($file_name) . '"';
} else {
$con_dis = 'Content-Disposition: attachment; filename="' . basename($file_name) . '"';
}
$file = fopen($file_name, "r");
//输入文件标签
ob_end_clean();Header("Content-type: application/octet-stream");Header("Accept-Ranges: bytes");Header("Accept-Length: ".filesize($file_name));Header($con_dis);
//输出文件内容
//读取文件内容并直接输出到浏览器
echo fread($file, filesize($file_name));@fclose($file);
exit(0);
}
*/
/*
echo $_SERVER["HTTP_USER_AGENT"];
echo "<br/>";
echo $_SERVER['DOCUMENT_ROOT'];
*/
?>

php下载文件的一种方式的更多相关文章

  1. 从后端接口下载文件的2种方式:get方式、post方式

    从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx&params2=xxxx' ...

  2. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  3. Asp.Net 下载文件的几种方式

    asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...

  4. Java下载文件的几种方式

    转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...

  5. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

  6. asp.net 浏览器下载文件的四种方式

    // 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...

  7. java 从网上下载文件的几种方式

    package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import ...

  8. C#从服务器下载文件的四种方式

    //方法一:TransmitFile实现下载 string fileName = "ss.docx"; //客户端预设的文件名,导出时可修改  string filePath = ...

  9. asp.net mvc 上传下载文件的几种方式

    view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...

随机推荐

  1. LeetCode OJ 1. Two Sum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. UVA 624 CD (01背包)

    //路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: ...

  3. $and $not null 正则表达式

    查询MasterID大于1且MasterType等于TestType的文档: db.SysCore.find({$and:[{"MasterID":{$gt:1}},{" ...

  4. 基于多线程多用户的FTP服务器与客户端功能实现

    项目介绍: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp server上随意切换目录 允许用户查看当 ...

  5. ECOS-Ecstore 伪静态规则

    .htaccess 文件 RewriteEngine On RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME ...

  6. 关于oracle数据库(8)查询2

    筛选数据,直接加where条件,并且and,或者or 使用rownum获取前N条数据 select * from 表名 where rownum <= 数字; 如:获取前5条数据 select ...

  7. YaHoo Web优化的14条法则

    Web应用性能优化黄金法则:先优化前端程序(front-end)的性能,因为这是80%或以上的最终用户响应时间的花费所在. 法则1. 减少HTTP请求次数 80%的最终用户响应时间花在前端程序上,而其 ...

  8. CodeFroces--Good Bye 2016-A-New Year and Hurry(水题-模拟)

    A. New Year and Hurry time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. VirtualBox 不能为虚拟电脑打开一个新的任务 可能的解决方案

    1. 在虚拟机上右键,清除保存状态 2.Cannot load R0 module C:\Program Files\Oracle\VirtualBox/VBoxDD2R0.r0: SUPR3Load ...

  10. Bootstrap 3 与 Foundation 5

    开发工程师, 使用 Bootstrap. 前端开发人员, 使用 Foundation. 我们来谈谈为什么. Bootstrap 与 Foundation 有许多关键的区别, 但是, 我想你只需要记住一 ...