<?php 
/** 
* 下载文件 
* 
*/ 
class download{ 
protected $_filename; 
protected $_filepath; 
protected $_filesize;//文件大小 
public function __construct($filename){ 
$this->_filename=$filename; 
$this->_filepath=dirname(__FILE__).'https://www.jb51.net/'.$filename; 
} 
//获取文件名 
public function getfilename(){ 
return $this->_filename; 
} 
//获取文件路径(包含文件名) 
public function getfilepath(){ 
return $this->_filepath; 
} 
//获取文件大小 
public function getfilesize(){ 
return $this->_filesize=number_format(filesize($this->_filepath)/(1024*1024),2);//去小数点后两位 
} 
//下载文件的功能 
public function getfiles(){ 
//检查文件是否存在 
if (file_exists($this->_filepath)){ 
//打开文件 
$file = fopen($this->_filepath,"r"); 
//返回的文件类型 
Header("Content-type: application/octet-stream"); 
//按照字节大小返回 
Header("Accept-Ranges: bytes"); 
//返回文件的大小 
Header("Accept-Length: ".filesize($this->_filepath)); 
//这里对客户端的弹出对话框,对应的文件名 
Header("Content-Disposition: attachment; filename=".$this->_filename); 
//修改之前,一次性将数据传输给客户端 
echo fread($file, filesize($this->_filepath)); 
//修改之后,一次只传输1024个字节的数据给客户端 
//向客户端回送数据 
$buffer=1024;// 
//判断文件是否读完 
while (!feof($file)) { 
//将文件读入内存 
$file_data=fread($file,$buffer); 
//每次向客户端回送1024个字节的数据 
echo $file_data; 
} 
fclose($file); 
}else { 
echo "<script>alert('对不起,您要下载的文件不存在');</script>"; 
} 
} 
} 
?> 
页面显示的代码:
复制代码 代码如下:
 
<script type="text/javascript" src="https://www.jb51.net/jquery-1.7.2.js"></script> 
<script type="text/javascript" src="https://www.jb51.net/ajax.js"></script> 
<?php 
header("Content-type:text/html;charset=utf8"); 
require('./getfile.php'); 
$scandir=new traverseDir(); 
$scandir->scandir($scandir->currentdir); 
$scandir->currentdir; 
if (isset($_POST['down_load'])){ 
$items=$_POST['items']; 
$scandir->tozip($items);//将文件压缩成zip格式 
} 
echo "当前的工作目录:".$scandir->currentdir; 
echo "<br>当前目录下的所有文件"; 
?> 
<form action="list.php" method="POST"> 
<table> 
<tr> 
<td></td> 
<td>名称</td> 
<td>大小(KB)</td> 
</tr> 
<?php 
$res=$scandir->fileinfo; 
foreach ($res as $k=>$v){ 
if (!($k=='.' || $k=='..')) {//过滤掉.和.. 
?> 
<tr> 
<td><input type="checkbox" value="<?php echo $k;?>"></td> 
<td><?php echo $k; ?></td> 
<td><?php echo number_format($v[0],0); ?></td> 
</tr> 
<?php 
} 
} 
?> 
<tr> 
<td><input type="checkbox"><label for="selall">全选</label></td> 
<td><input type="submit" value="打包并下载"></td> 
</tr> 
</table> 
</form> 
您可能感兴趣的文章:
