
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
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();
?>
推荐学习:《PHP视频教程》
以上就是php保存文件的方法的详细内容,更多请关注亿码酷站其它相关文章!
php保存文件的方法
—–文章转载自PHP中文网如有侵权请联系ymkuzhan@126.com删除
转载请注明来源:php保存文件的方法
本文永久链接地址:https://www.ymkuzhan.com/31488.html
本文永久链接地址:https://www.ymkuzhan.com/31488.html
下载声明:
本站资源如无特殊说明默认解压密码为www.ymkuzhan.com建议使用WinRAR解压; 本站资源来源于用户分享、互换、购买以及网络收集等渠道,本站不提供任何技术服务及有偿服务,资源仅提供给大家学习研究请勿作它用。 赞助本站仅为维持服务器日常运行并非购买程序及源码费用因此不提供任何技术支持,如果你喜欢该程序,请购买正版! 版权声明:
下载本站资源学习研究的默认同意本站【版权声明】若本站提供的资源侵犯到你的权益,请提交版权证明文件至邮箱ymkuzhan#126.com(将#替换为@)站长将会在三个工作日内为您删除。 免责声明:
您好,本站所有资源(包括但不限于:源码、素材、工具、字体、图像、模板等)均为用户分享、互换、购买以及网络收集而来,并未取得原始权利人授权,因此禁止一切商用行为,仅可用于个人研究学习使用。请务必于下载后24小时内彻底删除,一切因下载人使用所引起的法律相关责任,包括但不限于:侵权,索赔,法律责任,刑事责任等相关责任,全部由下载人/使用人,全部承担。以上说明,一经发布视为您已全部阅读,理解、同意以上内容,如对以上内容持有异议,请勿下载,谢谢配合!支持正版,人人有责,如不慎对您的合法权益构成侵犯,请联系我们对相应内容进行删除,谢谢!


