php遍历上级目录、压缩上级目录保存到指定文件夹
taotaoit PHP 2019-09-17 2498 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6553206 2620 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)
打开边栏(ESC) 关闭边栏(ESC)

该文章由

PHP自带的ZipArchive压缩文件并下载打包好的文件http://taotaoit.com/article/details/804.html

修改而来

实现:

可以遍历上级目录,遍历二级三级目录,压缩指定文件或文件夹到指定文件夹。

压缩20多M的文件下载正常,压缩70多M的文件,下载出错,具体原因不知,大文件可以注释下载代码,改为ftp下载

list.php文件代码:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<?php
header("Content-type:text/html;charset=utf8");
require('./getfile.php');
$path = isset($_GET['path']) ? $_GET['path'] : '../..'; //遍历目录
$zippath = '../../backup/'; //压缩文件保存目录
$scandir=new traverseDir();
//$scandir->scandir($scandir->currentdir);
$scandir->scandir($path);
$scandir->currentdir;

if (isset($_POST['down_load'])){ 
	$items=$_POST['items'];
	$scandir->tozip($items,$path,$zippath);//将文件压缩成zip格式
} 
//echo "当前的工作目录:".$scandir->currentdir;
echo "当前目录:".$path;
if($path != '..'){
	echo '<br><a href="?path=' . dirname($path) . '">上级目录</a>';
}
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" name="items[]" class="filename" value="<?php echo $k;?>"></td>
        <td>
			<?php 
			if($v['type'] == 1){
				echo '文件夹:<a href="?path=' . $v["path"] . '">' . $k . '</a>';
			}else{
				echo $k;
			}
			?>
		</td>
        <td>
        <?php 
        echo number_format($v[0],0);
        ?></td>
    </tr>
<?php
        }
    }
?>
    <tr>
        <td><input type="checkbox" id="selall"><label for="selall">全选</label></td>
        <td><input type="submit" name="down_load" value="备份" id="tozip_tetttt"></td>
    </tr>
</table>
</form>
<script type="text/javascript">
$("#selall").click(function(){
    var xz = $(this).prop("checked");//判断全选按钮的选中状态
    var ck = $(".filename").prop("checked",xz); //让class名为qx的选项的选中状态和全选按钮的选中状态一致。
});
</script>

getfile.php文件代码:

<?php
require'./download.php';
/**
 * 遍历目录,打包成zip格式
 */
class traverseDir{
    public $currentdir;//当前目录
    public $filename;//文件名
    public $fileinfo;//用于保存当前目录下的所有文件名和目录名以及文件大小
    public function __construct(){
        $this->currentdir=getcwd();//返回当前目录
    }        
    //遍历目录
    public function scandir($filepath){
        if (is_dir($filepath)){
			$arr=scandir($filepath);
			foreach ($arr as $k=>$v){
				$v2=iconv("gb2312","utf-8",$v); // 文件或目录为中文,显示乱码,因为页面是utf-8编码,从windows获得的文件名称是gb2312编码,所以要把文件名gb2312转出utf-8编码,输出到页面。但是getfilesize($v)也用到文件名获取文件大小,如果转成utf-8编码后,就找不到文件了,所以getfilesize($v)用的还是原来的gb2312编码,这也是上面变量命名为$v2的原因。关联本页面72行
				$filepath_new = $filepath . '/' . $v;
				if(!is_dir($filepath_new)){
					$this->fileinfo[$v2][]=$this->getfilesize($filepath_new);
					$this->fileinfo[$v2]['type'] = 0;
					$this->fileinfo[$v2]['path'] = $filepath_new;
				}else{
					$this->fileinfo[$v2][]=0;
					$this->fileinfo[$v2]['type'] = 1;
					$this->fileinfo[$v2]['path'] = $filepath_new;
				}				
			}
		}else {
			echo "<script>alert('当前目录不是有效目录');</script>";
		}
    }
    /**
     * 返回文件的大小
     * @param string $filename 文件名
     * @return 文件大小(KB)
     */
    public function getfilesize($fname){
        return filesize($fname)/1024;
    }
    /**
     * 压缩目录
     */
    public function addFileToZip($path, $zip) {
		// 判断是文件还是目录,区别对待
        if(is_dir($path)){
            $handler = opendir($path); //打开当前文件夹由$path指定。
            while (($filename = readdir($handler)) !== false) {
                if ($filename != "." && $filename != "..") { // 文件夹文件名字为'.'和‘..’,不要对他们进行操作
					if (is_dir($path . "/" . $filename)) { // 如果读取的某个对象是文件夹,则递归
                        $this->addFileToZip($path . "/" . $filename, $zip);
                    } else { // 将文件加入zip对象
                        $zip->addFile($path . "/" . $filename);
                    } 
                } 
            } 
            @closedir($path);
        }else{
            $zip->addFile($path);
        }
    } 
    /**
     * 压缩文件(zip格式)
     */
    public function tozip($items,$path,$zippath){ 
        $zip=new ZipArchive();
        $zipname = date('YmdHis',time()).'.zip';
        if (!file_exists($zippath.$zipname)){
            $zip->open($zippath.$zipname,ZipArchive::CREATE | ZipArchive::OVERWRITE);//创建一个空的zip文件         
            for ($i=0;$i<count($items);$i++){
				$v2=iconv("utf-8","gb2312",$items[$i]); // 由于遍历目录的时候,把文件夹名称转换成utf-8输出到页面的,所以通过表单提交的文件名是utf-8编码的,而utf-8编码的文件名在windows中是找不到对于文件的,所以要把文件名转换成gb2312编码。关联本页面17行
				$this->addFileToZip($path.'/'.$v2, $zip);               
            }
            $zip->close();
			//压缩20多M的文件下载正常,压缩70多M的文件,下载出错,具体原因不知,大文件可以注释下载代码,改为ftp下载
			$dw=new download($zipname,$zippath); //下载文件
			$dw->getfiles();
			//echo "<script>alert('备份完成');</script>";
        }
    }
}
?>

download.php文件代码:

<?php
/**
 * 下载文件
 */
class download{
    protected $_filename;
    protected $_filepath;
    protected $_filesize;//文件大小
    public function __construct($filename,$filepath){
        $this->_filename=$filename;
        $this->_filepath=dirname(__FILE__).'/'.$filepath.$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>";
        }
    }
}

?>

版权声明:本站原创文章,允许自由转载。

相关推荐
php实现ZIP压缩文件解压缩,中文乱码解决方法(重要)
PHP | 2019-04-30 5700
直接上代码,具体代码里面都有注释。直接中文压缩文件解压到中文文件夹。 <?php // 需开启配置 php_zip.dll // phpinfo(); header("Content-type:text/html;charset=utf-8"); /* * $filename 被解压文件名 * $path 解压...
openssl_private_decrypt解密失败
PHP | 2019-01-16 5655
复制别人的解密程序,原程序可以解密,复制过来就不可以 一步步尝试发现,秘钥换行符有区别, 原秘钥有换行符,复制过来不知道什么时候把换行符清空了,成了一行字符串了。
评论:0条
评论加载中...
发表评论