>  기사  >  백엔드 개발  >  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으로 문의하세요.