php保存文件的方法:首先创建一个PHP示例文件;然后生成文件;最后用代码“if(isset($_GET["filepath"])) {...}”实现下载保存即可。
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
php保存文件的方法
PHP 下载保存文件到本地
经常需要点击按钮,然后弹出一个对话框,保存下载文件。
最常见的方式,就用链接实现,例如:
<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脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用