傅令江的光影色彩世界
留住记忆的点滴
  • 首页
  • 文学
    • 诗词鉴赏
    • 美文共读
    • 原创
  • 编程
    • php
    • asp
    • .net
    • VB
    • C/C++
    • 易语言
    • js
    • 其他
    • 逆向
  • 运维
    • windows
    • linux
  • 光影色彩
    • 电影
    • 音乐
  • 科技
    • 互联网
    • 手机
  • 生活
    • 情感
  • 微语
7月122022

PHP网站备份程序

作者:傅令江   发布:2022-7-12 11:16   分类:php   阅读:1879次   评论:0条  
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title>网站程序备份</title>
</head> 
<body> 
<form name="myform" method="post" action="">
<?php
    error_reporting(E_ALL & ~E_NOTICE); 
    ini_set('memory_limit', '2048M');
    echo "选择要压缩的文件或目录:<br>"; 
    $fdir = opendir('./'); 
    while($file=readdir($fdir))
    { 
        if($file=='.'|| $file=='..')
        continue; 
        echo "<input name='dfile[]' type='checkbox' value='$file' ".($file==basename(__FILE__)?"":"checked")."> "; 
  
        if(is_file($file))
        { 
            echo "<font face="wingdings" size="5">2</font>&nbsp;&nbsp;$file<br>"; 
        }
        else
        { 
            echo "<font face="wingdings" size="5">0</font>&nbsp;$file<br>"; 
        } 
    } 
?> 
<br>
包含下列文件类型:
<input name="file_type" type="text" id="file_type" value="" size="50"> 
<font color="red">
(文件类型用"|"隔开,默认空则包含任意文件,例:如果需要打包php和jpg文件,则输入"php|jpg")
</font>
<br>
压缩文件保存到目录: 
<input name="todir" type="text" id="todir" value="__dwb2011__" size="15"> 
<font color="red">
(留空为本目录,必须有写入权限)
</font>
<br> 
压缩文件名称: 
<input name="zipname" type="text" id="zipname" value="dwb2011.zip" size="15"> 
<font color="red">
(.zip)
</font>
<br> 
<br>
<input name="myaction" type="hidden" id="myaction" value="dozip"> 
<input type='button' value='反选' onclick='selrev();'> 
<input type="submit" name="Submit" value=" 开始压缩 "> 
<script language='javascript'> 
    function selrev()
    { 
        with(document.myform)
        { 
            for(i=0;i<elements.length;i++)
            { 
                thiselm = elements[i]; 
                if(thiselm.name.match(/dfile[]/))
                    thiselm.checked = !thiselm.checked; 
            } 
        } 
    } 
</script> 
  
<?php
error_reporting(E_ALL & ~E_NOTICE);
set_time_limit(0); 
class PHPzip
{ 
    var $file_count = 0 ; 
    var $datastr_len   = 0; 
    var $dirstr_len = 0; 
    var $filedata = ''; //该变量只被类外部程序访问 
    var $gzfilename; 
    var $fp; 
    var $dirstr=''; 
    var $filefilters = array();
  
    function SetFileFilter($filetype)
    {
        $this->filefilters = explode('|',$filetype);
    }
  
    //返回文件的修改时间格式. 
    //只为本类内部函数调用. 
    function unix2DosTime($unixtime = 0)
    { 
        $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); 
        if ($timearray['year'] < 1980)
        { 
            $timearray['year']    = 1980; 
            $timearray['mon']     = 1; 
            $timearray['mday']    = 1; 
            $timearray['hours']   = 0; 
            $timearray['minutes'] = 0; 
            $timearray['seconds'] = 0; 
        } 
        return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |  ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1); 
    } 
  
    //初始化文件,建立文件目录, 
    //并返回文件的写入权限. 
    function startfile($path = 'dodo.zip')
    { 
        $this->gzfilename=$path; 
        $mypathdir=array(); 
  
        do
        { 
            $mypathdir[] = $path = dirname($path); 
        }    while($path != '.'); 
  
        @end($mypathdir); 
  
        do
        { 
            $path = @current($mypathdir); 
            @mkdir($path); 
        }    while(@prev($mypathdir)); 
  
        if($this->fp=@fopen($this->gzfilename,"w"))
        { 
            return true; 
        } 
  
        return false; 
    } 
  
    //添加一个文件到 zip 压缩包中. 
    function addfile($data, $name)
    { 
        $name = str_replace('\', '/', $name);
        if(strrchr($name,'/')=='/')
            return $this->adddir($name);
        if(!empty($this->filefilters))
        {
            if (!in_array(end(explode(".",$name)), $this->filefilters))
            {
                return;
            }
        }
        $dtime = dechex($this->unix2DosTime());
        $hexdtime = 'x' . $dtime[6] . $dtime[7] . 'x' . $dtime[4] . $dtime[5] . 'x' . $dtime[2] . $dtime[3] . 'x' . $dtime[0] . $dtime[1];
        eval('$hexdtime = "' . $hexdtime . '";');
  
        $unc_len = strlen($data);
        $crc     = crc32($data);
        $zdata   = gzcompress($data);
        $c_len   = strlen($zdata);
        $zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
  
        //新添文件内容格式化:
        $datastr  = "x50x4bx03x04";
        $datastr .= "x14x00";            // ver needed to extract
        $datastr .= "x00x00";            // gen purpose bit flag
        $datastr .= "x08x00";            // compression method
        $datastr .= $hexdtime;             // last mod time and date
        $datastr .= pack('V', $crc);             // crc32
        $datastr .= pack('V', $c_len);           // compressed filesize
        $datastr .= pack('V', $unc_len);         // uncompressed filesize
        $datastr .= pack('v', strlen($name));    // length of filename
        $datastr .= pack('v', 0);                // extra field length
        $datastr .= $name;
        $datastr .= $zdata;
        $datastr .= pack('V', $crc);                 // crc32
        $datastr .= pack('V', $c_len);               // compressed filesize
        $datastr .= pack('V', $unc_len);             // uncompressed filesize
        fwrite($this->fp,$datastr); //写入新的文件内容
        $my_datastr_len = strlen($datastr);
        unset($datastr);
  
        //新添文件目录信息
        $dirstr  = "x50x4bx01x02";
        $dirstr .= "x00x00";                 // version made by
        $dirstr .= "x14x00";                 // version needed to extract
        $dirstr .= "x00x00";                 // gen purpose bit flag
        $dirstr .= "x08x00";                 // compression method
        $dirstr .= $hexdtime;                  // last mod time & date
        $dirstr .= pack('V', $crc);            // crc32
        $dirstr .= pack('V', $c_len);          // compressed filesize
        $dirstr .= pack('V', $unc_len);        // uncompressed filesize
        $dirstr .= pack('v', strlen($name) );  // length of filename
        $dirstr .= pack('v', 0 );              // extra field length
        $dirstr .= pack('v', 0 );              // file comment length
        $dirstr .= pack('v', 0 );              // disk number start
        $dirstr .= pack('v', 0 );              // internal file attributes
        $dirstr .= pack('V', 32 );             // external file attributes - 'archive' bit set
        $dirstr .= pack('V',$this->datastr_len ); // relative offset of local header
        $dirstr .= $name;
        $this->dirstr .= $dirstr; //目录信息
        $this -> file_count ++;
        $this -> dirstr_len += strlen($dirstr);
        $this -> datastr_len += $my_datastr_len;
    }
  
    function adddir($name)
    {
        $name = str_replace("\", "/", $name);
        $datastr = "x50x4bx03x04x0ax00x00x00x00x00x00x00x00x00";
        $datastr .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) );
        $datastr .= pack("v", 0 ).$name.pack("V", 0).pack("V", 0).pack("V", 0);
        fwrite($this->fp,$datastr); //写入新的文件内容
        $my_datastr_len = strlen($datastr);
        unset($datastr);
        $dirstr = "x50x4bx01x02x00x00x0ax00x00x00x00x00x00x00x00x00";
        $dirstr .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) );
        $dirstr .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 );
        $dirstr .= pack("V", 16 ).pack("V",$this->datastr_len).$name;
        $this->dirstr .= $dirstr; //目录信息
        $this -> file_count ++;
        $this -> dirstr_len += strlen($dirstr);
        $this -> datastr_len += $my_datastr_len;
    }
  
    function createfile()
    {
        //压缩包结束信息,包括文件总数,目录信息读取指针位置等信息
        $endstr = "x50x4bx05x06x00x00x00x00" .
        pack('v', $this -> file_count) .
        pack('v', $this -> file_count) .
        pack('V', $this -> dirstr_len) .
        pack('V', $this -> datastr_len) .
        "x00x00";
        fwrite($this->fp,$this->dirstr.$endstr);
        fclose($this->fp);
    }
}
  
if(!trim($_REQUEST[zipname]))
    $_REQUEST[zipname] = "dodozip.zip";
else
    $_REQUEST[zipname] = trim($_REQUEST[zipname]);
  
if(!strrchr(strtolower($_REQUEST[zipname]),'.')=='.zip')
    $_REQUEST[zipname] .= ".zip";
  
$_REQUEST[todir] = str_replace('\','/',trim($_REQUEST[todir]));
  
if(!strrchr(strtolower($_REQUEST[todir]),'/')=='/')
    $_REQUEST[todir] .= "/";
  
if($_REQUEST[todir]=="/")
    $_REQUEST[todir] = "./";
  
function listfiles($dir=".")
{
    global $dodozip;
    $sub_file_num = 0;
    if(is_file("$dir"))
    {
        if(realpath($dodozip ->gzfilename)!=realpath("$dir"))
        {
            $dodozip -> addfile(implode('',file("$dir")),"$dir");
            return 1;
        }
        return 0;
    }
  
    $handle=opendir("$dir");
    while ($file = readdir($handle))
    {
        if($file=="."||$file=="..")
            continue;
  
        if(is_dir("$dir/$file"))
        {
            $sub_file_num += listfiles("$dir/$file");
        }
        else
        {
            if(realpath($dodozip ->gzfilename)!=realpath("$dir/$file"))
            {
                $dodozip -> addfile(implode('',file("$dir/$file")),"$dir/$file");
                $sub_file_num ++;
            }
        }
    }
  
    closedir($handle);
  
    if(!$sub_file_num)
        $dodozip -> addfile("","$dir/");
  
    return $sub_file_num;
}
  
function num_bitunit($num)
{
    $bitunit=array(' B',' KB',' MB',' GB');
    for($key=0;$key<count($bitunit);$key++)
    {
        if($num>=pow(2,10*$key)-1)
        { //1023B 会显示为 1KB
            $num_bitunit_str=(ceil($num/pow(2,10*$key)*100)/100)." $bitunit[$key]";
        }
    }
    return $num_bitunit_str;
}
  
if(is_array($_REQUEST[dfile]))
{
    $dodozip = new PHPzip;
    if($_REQUEST["file_type"] != NULL)
        $dodozip -> SetFileFilter($_REQUEST["file_type"]);
    if($dodozip -> startfile("$_REQUEST[todir]$_REQUEST[zipname]"))
    {
        echo "正在添加压缩文件...<br><br>";
        $filenum = 0;
        foreach($_REQUEST[dfile] as $file)
        {
            if(is_file($file))
            {
                if(!empty($dodozip -> filefilters))
                    if (!in_array(end(explode(".",$file)), $dodozip -> filefilters))
                        continue;
                echo "<font face="wingdings" size="5">2</font>&nbsp;&nbsp;$file<br>";
            }
            else
            {
                echo "<font face="wingdings" size="5">0</font>&nbsp;$file<br>";
            }
            $filenum += listfiles($file);
        }
        $dodozip -> createfile();
  
        echo "<br>压缩完成,共添加 $filenum 个文件.<br><a href='$_REQUEST[todir]$_REQUEST[zipname]' _fcksavedurl='$_REQUEST[todir]$_REQUEST[zipname]'>$_REQUEST[todir]$_REQUEST[zipname] (".num_bitunit(filesize("$_REQUEST[todir]$_REQUEST[zipname]")).")</a>";
    }
    else
    {
        echo "$_REQUEST[todir]$_REQUEST[zipname] 不能写入,请检查路径或权限是否正确.<br>";
    }
}
?> 
</form>
</body> 
</html> 



本文固定链接: https://www.fulingjiang.cn/php/252.html

blogger
该日志由 傅令江 于2022-7-12 11:16 Tuesday发表在 php 分类下。
版权所有:《傅令江的光影色彩世界》 → 《PHP网站备份程序》;
除特别标注,本博客所有文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:
上一篇::PHP控制文件下载速度
下一篇:PHP文件加密

热门文章

  • 兄弟二周年祭

相关文章

  • 基于php一句话木马的变种总结
  • PHP WebShell变形技术总结
  • PHP性能之语言性能优化:vld——查看代码opcode的神器
  • php图片加水印类
  • php生成缩略图的类
取消回复

发表评论

亲,头像对么?

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


  • 日历

  • 存档

    • 2024年10月(1)
    • 2023年2月(1)
    • 2022年11月(1)
    • 2022年10月(10)
    • 2022年9月(13)
    • 2022年8月(2)
    • 2022年7月(14)
    • 2022年6月(2)
    • 2022年5月(8)
    • 2022年4月(7)
    • 2022年3月(13)
    • 2022年2月(2)
    • 2022年1月(9)
    • 2021年12月(2)
    • 2021年11月(4)
    • 2021年10月(2)
    • 2021年9月(6)
    • 2021年7月(4)
    • 2021年6月(3)
    • 2021年5月(3)
    • 2021年4月(11)
    • 2021年3月(13)
    • 2021年2月(2)
    • 2021年1月(1)
    • 2020年12月(1)
    • 2020年4月(5)
    • 2019年9月(1)
    • 2019年8月(1)
    • 2019年5月(3)
    • 2018年3月(1)
    • 2017年10月(1)
    • 2016年7月(1)
    • 2016年4月(1)
    • 2015年12月(1)
    • 2015年11月(3)
    • 2015年9月(1)
    • 2015年8月(10)
    • 2015年7月(1)
    • 2015年6月(1)
    • 2015年4月(1)
    • 2015年3月(3)
    • 2015年2月(8)
    • 2015年1月(4)
    • 2014年12月(1)
    • 2014年11月(27)
    • 2014年10月(13)
    • 2014年9月(14)
    • 2014年8月(26)
    • 2014年7月(21)
  • 最新评论

    • 令狐江:
      喜欢这首歌是因为可以引起共鸣!
  • 链接

    • 演讲稿网
    • Recollect
    • 演讲稿
    • 祁阳人生活网
    • 我爱演讲稿网
  • 搜索

  • 标签

      函数 自定义方法 SEO 分页 分页函数 分页方法 nginx重新的一些规则
  • 分类

    • 文学(0)
    • 编程(0)
    • 运维(0)
    • 光影色彩(0)
    • 科技(0)
    • 生活(0)
    • 诗词鉴赏(3)
    • 美文共读(1)
    • 原创(10)
    • php(111)
    • asp(1)
    • .net(0)
    • VB(0)
    • C/C++(0)
    • 易语言(0)
    • js(8)
    • 其他(9)
    • 逆向(2)
    • windows(11)
    • linux(121)
    • 电影(0)
    • 音乐(1)
    • 互联网(4)
    • 手机(0)
    • 情感(2)
  • 最新文章热门文章随机文章

    • 兄弟二周年祭
    • openai给的ionCube 解密代码,应该是老版本可以这样
    • WordPress – 5秒盾防CC(PHP通用代码)
    • 我高中最好的朋友今天猝死了-伤心得不行
    • Linux系统中 systemd-journaldCPU占用异常的解决方法
    • SVN Skipped 'xxx' -- Node remains in conflict 错误的解决办法
    • 解决Linux读写nfs共享盘速度慢的问题
    • php 获取302跳转后的地址
    • 让vsftp显示隐藏文件的办法,比如显示 .htaccess
    • 添加自签名https证书到centos系统信任的问题
    • Layui数据表格渲染图片显示出来3种方法
    • PHP用socket检测服务器IP端口是否开放?
    • 适用于最新版6.2版本
    • 不同时间点跳转不同网址js
    • php多个图片合成一张图片
Copyright © 2001-2025 傅令江的光影色彩世界. Powered by www.fulingjiang.cn ICP备案:京ICP备14015190号-5