首页 » Web前端 » thinkphp32php技巧_基于thinkphp32的文件下载

thinkphp32php技巧_基于thinkphp32的文件下载

访客 2024-12-03 0

扫一扫用手机浏览

文章目录 [+]

整合FileToZip类。
有改动!
有问题可在评论向我咨询

1 PHP 部分,查询所要下载的文件路径,并调用下载类库,就可以直接下载了

thinkphp32php技巧_基于thinkphp32的文件下载 thinkphp32php技巧_基于thinkphp32的文件下载 Web前端

2 FileToZip类库(有改动)

thinkphp32php技巧_基于thinkphp32的文件下载 thinkphp32php技巧_基于thinkphp32的文件下载 Web前端
(图片来自网络侵删)

<?php

class traverseDir{

public $currentdir;//当前目录

public $filename;//文件名

public $fileinfo;//用于保存当前目录下的所有文件名和目录名以及文件大小

public $savepath;

public function __construct($curpath,$savepath){

$this->currentdir=$curpath;//返回当前目录

$this->savepath=$savepath;//返回当前目录

}

//遍历目录

public function scandir($filepath){

if (is_dir($filepath)){

$arr=scandir($filepath);

foreach ($arr as $k=>$v){

$this->fileinfo[$v][]=$this->getfilesize($v);

}

}else {

echo \"大众<script>alert('当前目录不是有效目录');</script>\公众;

}

}

/

返回文件的大小

@param string $filename 文件名

@return 文件大小(KB)

/

public function getfilesize($fname){

return filesize($fname)/1024;

}

/

压缩文件(zip格式)

/

public function tozip($items){

$zip=new ZipArchive();

$zipname=date('YmdHis',time());

if (!file_exists($zipname)){

/

办理报错:ZipArchive::close(): Invalid or uninitialized Zip

/

$zip->open($this->savepath.$zipname.'.zip', ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);

//文件路径模式

for ($i=0;$i<count($items);$i++){

$zip->addFile($items[$i],basename($items[$i]));

}

$zip->close();

$dw=new download($zipname.'.zip',$this->savepath); //下载文件

$dw->getfiles();

unlink($this->savepath.$zipname.'.zip'); //下载完成后要进行删除

}

}

}

/

下载文件

/

class download{

protected $_filename;

protected $_filepath;

protected $_filesize;//文件大小

protected $savepath;//文件大小

public function __construct($filename,$savepath){

$this->_filename=$filename;

$this->_filepath=$savepath.$filename;

}

//获取文件名

public function getfilename(){

return $this->_filename;

}

//获取文件路径(包含文件名)

public function getfilepath(){

return $this->_filepath;

}

//获取文件大小

public function getfilesize(){

return $this->_filesize=number_format(filesize($this->_filepath)/(10241024),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('sorry,file does not exist!');history.back(-1);</script>\"大众;

}

}

}

?>

标签:

相关文章

phpfpmalpine技巧_PHP 8 能有多快

作者 | Pedro Escudero译者 | 弯月,责编 | 郭芮以下为译文:今年年底,PHP-8即将发布。虽然这个主版本有很多...

Web前端 2024-12-05 阅读0 评论0