首页  >  文章  >  后端开发  >  php 下载保存文件保存到本地的两种方法_PHP教程

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

WBOY
WBOY原创
2016-07-20 11:15:351086浏览

第一种:

<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...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn