php下载文件的一种方式
<?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下载文件的一种方式的更多相关文章
- 从后端接口下载文件的2种方式:get方式、post方式
从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx¶ms2=xxxx' ...
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- Asp.Net 下载文件的几种方式
asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...
- Java下载文件的几种方式
转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...
- java 下载文件的两种方式和java文件的上传
一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...
- asp.net 浏览器下载文件的四种方式
// 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...
- java 从网上下载文件的几种方式
package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import ...
- C#从服务器下载文件的四种方式
//方法一:TransmitFile实现下载 string fileName = "ss.docx"; //客户端预设的文件名,导出时可修改 string filePath = ...
- asp.net mvc 上传下载文件的几种方式
view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...
随机推荐
- shp文件显示
开发环境 Win7, VS2010 Sp1 QGIS 2.01 #include <qgsapplication.h> #include <qgsproviderregistry.h ...
- ashx入侵
<%@ WebHandler Language="C#" Class="TextLd" %>using System;using System.Co ...
- LeetCode OJ 59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- SDK does not contain any platforms. error (android)
By default sdk was installed under the C:\Users\<user_name>\AppData\Local\Android\sdk\ directo ...
- setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientific Linux
This is a guide on setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientif ...
- linux下ClamAV使用
第一步:Clamav下载http://www.clamav.net/downloads#yuminstall wget –y第二步:创建clamav用户和组groupaddclamav (创建cl ...
- java编写简单的累加程序
编程思路:1.建立类包demo: 2.在类包中建立CommanParameter类: 3.利用for循环通过强制类型转换将在后台中输入的String类型的字符转换为整型并进进累加操作: package ...
- 杭电OJ分类
基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1 ...
- Load PE from memory(反取证)(未完)
Article 1:Loading Win32/64 DLLs "manually" without LoadLibrary() The most important step ...
- django搭建Bootstrap常用问题解决方法
1.进入页面,提示Creating a ModelForm without either the 'fields' attribute or the 'exclude'时 解决方法:打开forms.p ...