php 下载保存文件保存到本地的两种方法_PHP教程

WBOY
發布: 2016-07-20 11:15:35
原創
1084 人瀏覽過

第一种:

<span  1</span> <?<span php 
</span><span  2</span> <span function</span><span  downfile()
</span><span  3</span> <span {
</span><span  4</span> <span $filename</span>=<span realpath</span>("resume.html"); <span //</span><span 文件名</span>
<span  5</span> <span $date</span>=<span date</span>("Ymd-H:i:m"<span );
</span><span  6</span> <span Header</span>( "Content-type: application/octet-stream "<span ); 
</span><span  7</span> <span Header</span>( "Accept-Ranges: bytes "<span ); 
</span><span  8</span> <span Header</span>( "Accept-Length: " .<span filesize</span>(<span $filename</span><span ));
</span><span  9</span> <span header</span>( "Content-Disposition: attachment; filename= {<span $date</span>}.doc"<span ); 
</span><span 10</span> <span echo</span> <span file_get_contents</span>(<span $filename</span><span );
</span><span 11</span> <span readfile</span>(<span $filename</span><span ); 
</span><span 12</span> <span }
</span><span 13</span> <span downfile();
</span><span 14</span> ?>
登入後複製

<?<span php 
</span><span //</span><span 下载文件保存到本地
//www.jbxue.com</span>
<span function</span> downfile(<span $fileurl</span><span )
{
</span><span ob_start</span><span (); 
</span><span $filename</span>=<span $fileurl</span><span ;
</span><span $date</span>=<span date</span>("Ymd-H:i:m"<span );
</span><span header</span>( "Content-type: application/octet-stream "<span ); 
</span><span header</span>( "Accept-Ranges: bytes "<span ); 
</span><span header</span>( "Content-Disposition: attachment; filename= {<span $date</span>}.doc"<span ); 
</span><span $size</span>=<span readfile</span>(<span $filename</span><span ); 
</span><span header</span>( "Accept-Length: " .<span $size</span><span );
}
</span><span $url</span>="url地址"<span ;
downfile(</span><span $url</span><span );
</span>?>
登入後複製

<span  1</span> <?<span php 
</span><span  2</span> <span //</span><span 下载文件保存至本地
</span><span  3</span> <span //www.jbxue.com</span>
<span  4</span> <span function</span> downfile(<span $fileurl</span><span )
</span><span  5</span> <span {
</span><span  6</span> <span $filename</span>=<span $fileurl</span><span ;
</span><span  7</span> <span $file</span> = <span fopen</span>(<span $filename</span>, "rb"<span ); 
</span><span  8</span> <span Header</span>( "Content-type: application/octet-stream "<span ); 
</span><span  9</span> <span Header</span>( "Accept-Ranges: bytes "<span ); 
</span><span 10</span> <span Header</span>( "Content-Disposition: attachment; filename= 4.doc"<span ); 
</span><span 11</span> <span $contents</span> = ""<span ;
</span><span 12</span> <span while</span> (!<span feof</span>(<span $file</span><span )) {
</span><span 13</span> <span $contents</span> .= <span fread</span>(<span $file</span>, 8192<span );
</span><span 14</span> <span }
</span><span 15</span> <span echo</span> <span $contents</span><span ;
</span><span 16</span> <span fclose</span>(<span $file</span><span ); 
</span><span 17</span> <span }
</span><span 18</span> <span $url</span>="url地址"<span ;
</span><span 19</span> downfile(<span $url</span><span );
</span><span 20</span> ?>
登入後複製

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440197.htmlTechArticle第一种: ? = ("resume.html"); = ("Ymd-H:i:m" ( "Content-type: application/octet-stream " ( "Accept-Ranges: bytes " ( "Accept-Length: " . ( ( "Content-Disposition: attachment; f...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!