搜索
首页后端开发php教程使用bcompiler对PHP文件进行加密的代码_php技巧

使用说明:

//载入函式
include_once('phpCodeZip.php');
//建立加密文件(sourceDir要加密的php文件目录,targetDir加密后的文件目录)
$encryption = new PhoCodeZip('sourceDir','targetDir');
//执行行加密
$encryption->zip();

phpCodeZip.php源码下载
phpCodeZip.rar
phpCodeZip.php源码内容

复制代码 代码如下:

/*
* @license:MIT & GPL
*/
class PhpCodeZip{
//欲进行压缩加密的来源资料夹
var $sourceDir = ' .';
//进行压缩加密的存放的资料夹
var $targetDir = 'tmp';
//是否进行加密
var $bcompiler = true;
//是否去除空白注解断行
var $strip = true;
//来源资料夹档案路径阵列
var $sourcefilePaths = array();
//目的资料夹档案路径阵列
var $targetPaths = array();
//进行压缩加密前的资料夹大小
var $sizeBeforeZip = null;
//进行压缩加密后的资料夹大小
var $sizeAfterZip = null;
//断行的输出
var $newline = '';
/**
* 建构子
*
* @param string $sourceDir 来源资料夹
* @param string $targetDir 目的资料夹
* @param boolean $bcompiler 是否进行加密
* @param boolean $strip 是否去除空白注解断行
* @return boolean
*/
public function PhpCodeZip($sourceDir='.',$targetDir=' tmp',$bcompiler=true,$strip=true){
//配置初始变数
$this->sourceDir = $sourceDir;
$this->targetDir = $targetDir;
$this->bcompiler = $bcompiler;
//检查来源资料是否存在

if(!is_dir($this->sourceDir)){
die('指定的来源资料夹'.$this->sourceDir.'不存在,请重新设定');
} else {
//如果指定的目的资料夹存在,砍掉重练
if(is_dir ($this->targetDir)){
echo '【初始化目的地资料夹】'.$this->newline.$this->newline;
$this->cleanDir($this ->targetDir,true);
}
//建立与来源资料夹结构一样的目的资料夹
mkdir($this->targetDir,0777);
$dir_paths = $ this->getPaths($this->sourceDir,'*',GLOB_ONLYDIR);
foreach($dir_paths as $key => $path){
$path = explode('/',$ path);
$path[0] = $this->targetDir;
echo '=> '.join('/',$path).$this->newline;
mkdir (join('/',$path),0777);
}
//取得来源资料夹的档案路径清单
$this->sourcefilePaths = $this->getPaths($this ->sourceDir,'*');
//配对应目的地的档案路径清单
foreach($this->sourcefilePaths as $key => $path){
//设定目的资料夹档案路径
$path = explode('/',$path);
$path[0] = $this->targetDir;
$this->targetPaths[$key] = join('/',$path);
}
//记录执行前的资料夹大小
$this->sizeBeforeZip = $this->getSizeUnit($this->getDirSize ($this->sourceDir),2);
echo $this->newline.$this->newline;
}
}
/**
* 进行压缩加密
* @return boolean
*/
public function zip(){
$this->newline = '';
echo '【开始进行加密程序】(资料夹大小:'.$this->sizeBeforeZip.')'. $this->newline.$this->newline;
//将来源档案进行压缩
foreach($this->sourcefilePaths as $key => $path){
if( is_file($path)){
//取得档案资讯
$pathInfo = pathInfo($path);
echo '读取来源档:'.$path.$this->newline;
//取得压缩后的内容
echo '=>去除空白注解..........';
if($this->strip && $pathInfo['extension' ] == 'php'){
$fileAterZip = php_strip_whitespace($path);
} else {
$fileAterZip = file_get_contents($path);
}
echo '完毕'. $this->newline;

//取压缩后的内容写到目的位置
$fp = fopen($this->targetPaths[$key],'w ');
echo '=>写入目的档..........';
fwrite($fp,$fileAterZip);
fclose($fp);
echo '完毕' .$this->newline;
//是否若选择进行加密
if($this->bcompiler && $pathInfo['extension'] == 'php'){
echo '= >加密原始档..........';
//复制原始档
$fh = fopen($this->targetPaths[$key].'encrypt.php', "w");
bcompiler_write_header($fh);
bcompiler_write_file($fh, $this->targetPaths[$key]);
bcompiler_write_footer($fh);
fclose($fh );
//删除未加密的原始档

unlink($this->targetPaths[$key]);
//重新命名加密过后的档案

rename($this->targetPaths[$key].'encrypt.php',$this->targetPaths[$key]);
echo '完毕'.$this->newline;
}
echo $this->newline.$this->newline;
}
}
//重新计算压缩加密后的资料夹大小
$this->sizeAfterZip = $this->getSizeUnit($this->getDirSize($this->targetDir),2);
echo '【结束加密程序】'.$this->newline.$this->newline;

echo '《报告资讯》'.$this->newline;
echo '来源资料夹:'.$this->sourceDir.'('.$this->sizeBeforeZip.')'.$this->newline;
echo '目的资料夹:'.$this- >targetDir.'('.$this->sizeAfterZip.')'.$this->newline;
echo '档案大小增幅: '.$this->getSizeUnit(($this-> getDirSize($this->targetDir) - $this->getDirSize($this->sourceDir))).$this->newline;
echo '档案总数:'.count($this-> ;sourcefilePaths).'个'.$this->newline;

}
/**
* 删除目录夹所有档案
*
* @param string $dir 欲删除的资料夹
* @param boolean $deleteSelf 同时删除资料夹
* @return void
*/
private function cleanDir($dir='.',$deleteSelf= true){
if(!$dh = @opendir($dir)) return;
while (($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) $this->cleanDir($dir.'/'.$obj, true);
}
if ($deleteSelf){
closedir($dh);
@rmdir($dir);
}
}
/**
* 取得资料夹的总档案大小
*
* @param string $dir 欲剖析的资料夹
* @return int 位元组
*/
private function getDirSize($dir='.'){
//取得档案路径清单
$filePaths = $this->getPaths($dir,'*');
//初始化计数器
$sizeCounter = 0;
foreach($filePaths as $key => $path){
$sizeCounter = $sizeCounter filesize($path);
}
return ($sizeCounter);
}
/**
* 取得资料夹所有配对的路径
*
* @param string $start_dir 欲剖析的资料夹
* @return array 档案路径阵列
*/
private function getPaths($sDir, $sPattern, $nFlags = NULL){
$sDir = escapeshellcmd( $sDir);
$aFiles = glob("$sDir/$sPattern", $nFlags);
foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir) {
$aSubFiles = $this->getPaths($sSubDir, $sPattern, $nFlags);
$aFiles = array_merge($aFiles, $aSubFiles);
}
return $aFiles;
}
/**
* 档案大小单位转换函式
*
* @param int 档案大小
* @param int 小数点位数
* @param boolean 是否要将资料切成阵列
* @return mix 字串或阵列
*/
public function getSizeUnit($size,$decimal=2,$split=false){
//设定单位序列
$unit = array('Bytes', 'KB','MB','GB','TB','PB','EB','ZB','YB');
//初始化索引
$flag = 0;
//进行简化除算
while($size >= 1024){
$size = $size / 1024;
$flag ;
}
//是否要将数值与单位分开
if($split){
$sizeUnit = array(
'size' => number_format($size,$decimal),
'unit' => $unit[$ flag]
);
} else {
$sizeUnit = (number_format($size,$decimal)).$unit[$flag];
}
//回传大小与单位
return ($sizeUnit);
}
}
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)