您的位置:首页 > 编程语言 > PHP开发

php 实现下载文件

2015-12-02 15:14 615 查看
class DownLoadFile
{
public static function DownLoadFile1($filename)
{
$file_name=iconv("utf-8", "gb2312", $filename);
if (!file_exists($filename)){
echo "文件不存在!";
return;
}
$handle=fopen($filename, "r");
$filesize=filesize($filename);
header("Contect-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: $filesize");
header("Content-Disposition: attachment; filename=$filename");

$length = 1024;
$filecount=0;
while (!feof($handle) && ($filesize-$filecount)>0){
$filedata=fread($handle, $length);
$filecount+=$length;
echo $filedata;
}
fclose($handle);
}

public static function DownLoadFile2($file)
{
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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php