php怎么保存文件

這篇文章主要介紹了php怎么保存文件,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)同德免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

php保存文件的方法:首先創(chuàng)建一個(gè)PHP示例文件;然后生成文件;最后用代碼“if(isset($_GET["filepath"])) {...}”實(shí)現(xiàn)下載保存即可。

本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦

php保存文件的方法

PHP 下載保存文件到本地

經(jīng)常需要點(diǎn)擊按鈕,然后彈出一個(gè)對(duì)話框,保存下載文件。

最常見的方式,就用<a>鏈接實(shí)現(xiàn),例如:

<a href="xxx/youfile.txt"> youfile.txt </a>

本文介紹的下載保存方式,是通過生成文件后,然后用代碼實(shí)現(xiàn)下載保存。

完整示例(推薦)

<?php
/**
* 下載文件header函數(shù)
* 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> 是臨時(shí)文件已過期,服務(wù)器不保存!
                    <br><br>
                    請(qǐng)?zhí)崛∽钚麓恚?nbsp;<a href='../fetch.php'>http://proxy.mimvp.com/api/fetch.php</a>
                     
                    <!--
                    <script>
                    alert('" . $file_basename . "\\n是臨時(shí)文件,服務(wù)器不保存! \\n\\n請(qǐng)重新提取最新代理');
                    </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);
    }
 
    // 下載或取消后,刪除臨時(shí)文件
    $del_result = @unlink($res_filepath);
    if ($del_result == true) {
        @unlink($res_filepath);
    }
?>

網(wǎng)上其他方式

第一種:

<?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實(shí)現(xiàn)下載文件的兩種方法

方法1:

<?php
    /**
    * 下載文件, header函數(shù)實(shí)現(xiàn)
    */
 
    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函數(shù)的用法

方法2:

<?php
    //文件下載, readfile實(shí)現(xiàn)
    $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();
?>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“php怎么保存文件”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

當(dāng)前標(biāo)題:php怎么保存文件
當(dāng)前地址:http://muchs.cn/article2/gppooc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站制作、全網(wǎng)營銷推廣企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營