這篇文章主要介紹“php如何實(shí)現(xiàn)保存下載文件”,在日常操作中,相信很多人在php如何實(shí)現(xiàn)保存下載文件問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”php如何實(shí)現(xiàn)保存下載文件”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的十多年時(shí)間我們累計(jì)服務(wù)了上千家以及全國(guó)政企客戶,如墻體彩繪等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實(shí)力獲得客戶的一致表?yè)P(yáng)。
php實(shí)現(xiàn)保存下載文件的方法:1、通過“function downfile(){...}”方法實(shí)現(xiàn)下載文件;2、通過header函數(shù)實(shí)現(xiàn)保存下載。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php 下載保存文件保存到本地的兩種實(shí)現(xiàn)方法
這里的下載,指的是 彈出下載提示框。
第一種:
<?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(); ?>
或 (推薦這種方法,親測(cè)可行,其他的沒測(cè)試)
<?php function downfile($fileurl) { ob_start(); $filename=$fileurl; $date=date("Ymd-H:i:m"); $size=readfile($filename); header( "Content-type: application/octet-stream "); header( "Accept-Ranges: bytes "); header( "Content-Disposition: attachment; filename= {$date}.doc"); 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)下載文件的兩種方法。分享下,有用到的朋友看看哦。
方法一:
<?php /** * 下載文件 * header函數(shù) * */ 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ù)的用法。
方法二:
<?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(); ?>
到此,關(guān)于“php如何實(shí)現(xiàn)保存下載文件”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)頁(yè)標(biāo)題:php如何實(shí)現(xiàn)保存下載文件
文章轉(zhuǎn)載:http://chinadenli.net/article10/ppdhgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、建站公司、域名注冊(cè)、App開發(fā)、標(biāo)簽優(yōu)化、企業(yè)建站
聲明:本網(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)