PHP 下载保存文件到本地
PHP 下载保存文件到本地
经常需要点击按钮,然后弹出一个对话框,保存下载文件。
最常见的方式,就用<a>链接实现,例如:
<a href=”xxx/youfile.txt”> youfile.txt </a>
本文介绍的下载保存方式,是通过生成文件后,然后用代码实现下载保存。
完整示例(推荐)
<?php
/**
* 下载文件header函数
* copyright by www.mimvp.com
* 2015-05-10
*/$res_filepath = "";
if(isset($_GET["filepath"])) {$res_filepath = $_GET["filepath"];
}// $filepath = "./lib/tmp_txt_result_file_20150508170116.txt";$file_realpath = realpath($res_filepath);$file_basename = basename($res_filepath);
// $file_filesize = filesize($res_filepath);$file_fileinfo = pathinfo($res_filepath);if (!file_exists($res_filepath)){header("Content-type: text/html; charset=utf-8");echo "<html><div style='margin-left: 20px'><br><font color='blue'>$file_basename</font> 是临时文件已过期,服务器不保存!<br><br>请提取最新代理: <a href='../fetch.php'>http://proxy.mimvp.com/api/fetch.php</a><!--<script>alert('" . $file_basename . "\\n是临时文件,服务器不保存! \\n\\n请重新提取最新代理');</script>--></div></html>";} else {$file_filesize = filesize($res_filepath);$file = fopen($res_filepath, "r");Header("Content-type: application/octet-stream");Header("Accept-Ranges: bytes");Header("Accept-Length: " . $file_filesize);Header("Content-Disposition: attachment; filename=" . $file_basename);echo fread($file, $file_filesize);fclose($file);
// echo file_get_contents($filename);
// readfile($filename);}// 下载或取消后,删除临时文件$del_result = @unlink($res_filepath);if ($del_result == true) {@unlink($res_filepath);}
?>
网上其他方式
第一种:
<?php function downfile() {$filename=realpath("resume.html"); //文件名$date=date("Ymd-H:i:m");Header( "Content-type: application/octet-stream "); Header( "Accept-Ranges: bytes "); Header( "Accept-Length: " .filesize($filename));header( "Content-Disposition: attachment; filename= {$date}.doc"); echo file_get_contents($filename);readfile($filename); }downfile();
?>
或
<?php function downfile($fileurl) {ob_start(); $filename=$fileurl;$date=date("Ymd-H:i:m");header( "Content-type: application/octet-stream "); header( "Accept-Ranges: bytes "); header( "Content-Disposition: attachment; filename= {$date}.doc"); $size=readfile($filename); header( "Accept-Length: " .$size);}$url="url地址";downfile($url);
?>
第二种:
<?php function downfile($fileurl) {$filename=$fileurl;$file = fopen($filename, "rb"); Header( "Content-type: application/octet-stream "); Header( "Accept-Ranges: bytes "); Header( "Content-Disposition: attachment; filename= 4.doc"); $contents = "";while (!feof($file)) {$contents .= fread($file, 8192);}echo $contents;fclose($file); }$url="url地址";downfile($url);
?>
PHP实现下载文件的两种方法
方法1:
<?php/*** 下载文件, header函数实现*/header('Content-Description: File Transfer');header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename='.basename($filepath));header('Content-Transfer-Encoding: binary');header('Expires: 0′);header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);header('Pragma: public');header('Content-Length: ' . filesize($filepath));readfile($file_path);
?>
了解php中header函数的用法
方法2:
<?php//文件下载, readfile实现$fileinfo = pathinfo($filename);header('Content-type: application/x-'.$fileinfo['extension']);header('Content-Disposition: attachment; filename='.$fileinfo['basename']);header('Content-Length: '.filesize($filename));readfile($thefile);exit();
?>
经典实例:
http://proxy.mimvp.com/fetch.php (米扑代理提取,推荐)
PHP 下载保存文件到本地相关推荐
- php保存流文件到本地,php下载保存文件保存到本地的两种实现方法
第一种:<?php function downfile() { $filename=realpath("resume.html"); //文件名 $date=date(&qu ...
- python urlretrieve_使用urllib库的urlretrieve()方法下载网络文件到本地的方法
概述 见源码 源码 # !/usr/bin/env python # -*- coding:utf-8 -*- """ 图片(文件)下载,核心方法是 urllib.url ...
- android 下载保存视频到本地相册刷新 机型适配问题
android 下载保存视频到本地相册刷新 机型适配问题 android 下载保存视频到本地相册刷新问题 一般我们保存视频文件到本地 使用一下方法扫描到相册,通知相册更新 MediaScannerCo ...
- 微信小程序下载保存文件
前言 总是有需求想在微信小程序里面做下载文件并保存的功能,所以自己整理了一下小程序涉及到下载api,大致理了下在小程序里面下载的流程和解决方案. 一.涉及api 1.wx.saveFile() 文 ...
- R语言使用download.file函数下载网络文件到本地(Download File from the Internet)
R语言使用download.file函数下载网络文件到本地(Download File from the Internet) 目录 R语言使用download.file函数下载网络文件到本地(Down ...
- java 保存本地文件_java如何实现保存文件到本地
java如何实现保存文件到本地 发布时间:2020-04-30 11:20:24 来源:亿速云 阅读:773 作者:小新 java如何实现保存文件到本地?相信有很多人都不太了解,今天小编为了让大家更加 ...
- java接口保存文件到本地指定目录下
java接口保存文件到本地指定目录下.md 一.需求 二.后端代码 三.postman模拟页面上传 一.需求 通过接口将 file 保存到本地指定目录下 动动发财小手,关注 + 点赞 + 收藏不迷路. ...
- php远程下载到本地,PHP 下载远程文件到本地的简单示例
搜索热词 对PHP下载远程文件到本地存储的代码感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧! /** * PHP下载远程文件到本地存储的代码 * * @param * @a ...
- java下载网络文件至本地
通过url下载网络文件至本地 所需依赖和工具类代码 所需依赖 <dependency><groupId>org.apache.httpcomponents</groupI ...
- Jmeter下载保存文件,使用请求中中文文件名称
Jmeter下载文件保存为中文名称 前言 在Jmeter自动化中,通常我们会用到下载文件接口,然后Jmeter自带的没有下载保存文件功能,这时就需要使用beanshell帮我们处理 保存文件时我们常使 ...
最新文章
- JDK11使用IDEA,配置JavaFX
- 汇聚开发者星星之火,华为鸿蒙系统有希望成为国产之光?
- Standby Redo Log 的设定原则、创建、删除、查看、归档位置
- python代码画皮卡丘_程序员式优雅表白,教你用python代码画爱心
- appium的python教程_移动App Appium自动化测试教程Appium+Python 【2018年新】_IT教程网...
- 【翻译】How-To: Using the N* Stack, part 5
- SCI、EI、ISTP、ISR、SSCI、AHCI简介
- TensorFlow 深度学习中文第二版·翻译完成
- Netty工作笔记0045---Netty模型梳理
- error restore 01
- syncd的使用和部署
- L2-015 互评成绩(排序)
- 清理linux清理垃圾文件夹,让Ubuntu系统释放空间最有效的五种方法(清除不需要的或垃圾文件)...
- BZOJ 4300: 绝世好题 动态规划
- Android中字体文件位置
- 百度贴吧里如何留网址
- 给野生大熊猫当保镖,是种什么体验?
- python统计元音字母个数_python统计并输出字符串中小写元音字母的个数?
- 视频动态滚动水印制作,滚动的水印字幕
- matlab eye函数_[线性代数系列1] MATLAB入门笔记
热门文章
- cnzz统计代码被谷歌浏览器拦截警告
- numpy.take()用法
- 一文带你了解什么是CDN?
- 浏览器工作模式之标准模式/怪异模式/近似标准模式
- 古城钟楼的微博报时是如何实现的?[科普贴]
- aws ecs 通过efs挂载实现动态更新firelens日志配置
- docker wordpress 提示:Error establishing a database connection
- JS复制input内容
- pythonturtle画飞机_Python获取航线信息并且制作成图的讲解
- 【转载】100个思维模型(不一定都适用,各取所需)