首页 正文
244

PHP之文件下载

  • yiqingpeng
  • 2015-04-12
  • 0
  •  
提供本地文件供客户端下载:
方案一:
<?php
$str='Content-Disposition: attachment; filename="mypic.png"';
header("Content-type: application/force-download");
header($str);
readfile('./arrow.png');//特别注意在<?php ?>标签外不能有空白行。
?>

方案二:
$file  =  'monkey.gif' ;
if ( file_exists ( $file )) {
     header ( 'Content-Description: File Transfer' );
     header ( 'Content-Type: application/octet-stream' );
     header ( 'Content-Disposition: attachment; filename=' . basename ( $file ));
     header ( 'Content-Transfer-Encoding: binary' );
     header ( 'Expires: 0' );
     header ( 'Cache-Control: must-revalidate' );
     header ( 'Pragma: public' );
     header ( 'Content-Length: '  .  filesize ( $file ));
     ob_clean ();
     flush ();
     readfile ( $file ); 
    exit; 
} 

下载远程的文件(curl):
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $imageUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$img = curl_exec($ch);
curl_close($ch);
//$size = strlen($img); 
$fh=fopen($fileName, "a"); 
fwrite($fh,$img); 
fclose($fh);

正在加载评论...