php保存文件的方法:首先创建一个PHP示例文件;然后生成文件;最后用代码“if(isset($_GET["filepath"])) {...}”实现下载保存即可。
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
php保存文件的方法
PHP 下载保存文件到本地
经常需要点击按钮,然后弹出一个对话框,保存下载文件。
最常见的方式,就用3499910bf9dac5ae3c52d5ede7383485链接实现,例如:
<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(); ?>
推荐学习:《PHP视频教程》
以上是php保存文件的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

本文比较了酸和基本数据库模型,详细介绍了它们的特征和适当的用例。酸优先确定数据完整性和一致性,适合财务和电子商务应用程序,而基础则侧重于可用性和

本文讨论了确保PHP文件上传的确保,以防止诸如代码注入之类的漏洞。它专注于文件类型验证,安全存储和错误处理以增强应用程序安全性。

本文讨论了在PHP中实施API速率限制的策略,包括诸如令牌桶和漏水桶等算法,以及使用Symfony/Rate-limimiter之类的库。它还涵盖监视,动态调整速率限制和手

本文讨论了使用password_hash和pyspasswify在PHP中使用密码的好处。主要论点是,这些功能通过自动盐,强大的哈希算法和SECH来增强密码保护

本文讨论了OWASP在PHP和缓解策略中的十大漏洞。关键问题包括注射,验证损坏和XSS,并提供用于监视和保护PHP应用程序的推荐工具。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

Dreamweaver Mac版
视觉化网页开发工具