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

WBOY
Lepaskan: 2016-07-20 11:15:35
asal
1084 orang telah melayarinya

第一种:

<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> ?>
Salin selepas log masuk

<?<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>?>
Salin selepas log masuk

<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> ?>
Salin selepas log masuk

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...
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!