欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          php保存文件的方法

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

          php保存文件的方法

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

          php保存文件的方法

          PHP 下載保存文件到本地

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

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

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

          本文介紹的下載保存方式,是通過生成文件后,然后用代碼實現(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> 是臨時文件已過期,服務(wù)器不保存!                     <br><br>                     請?zhí)崛∽钚麓恚?<a href='../fetch.php'>http://proxy.mimvp.com/api/fetch.php</a>                                           <!--                     <script>                     alert('" . $file_basename . "\n是臨時文件,服務(wù)器不保存! \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);     } ?>

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

          方法1:

          <?php     /**     * 下載文件, header函數(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實現(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(); ?>

          推薦學(xué)習(xí):《PHP視頻教程》

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號