Instructions for use:
Load function
Include_once (' phpcodezip.php ');
Create an encrypted file (SourceDir the directory of PHP files to encrypt, targetdir the encrypted file directory)
$encryption = new Phocodezip (' SourceDir ', ' targetDir ');
Perform row encryption
$encryption->zip ();
phpcodezip.php Source Download
Phpcodezip.rar
phpcodezip.php Source Content
Copy CodeThe code is as follows:
/*
* @license: MIT & GPL
*/
Class phpcodezip{
To compress encrypted source folders
var $sourceDir = '. ';
Compressed encrypted storage folders
var $targetDir = ' tmp ';
Do you want to encrypt?
var $bcompiler = true;
Do you want to remove whitespace?
var $strip = true;
Source folder file path array
var $sourcefilePaths = array ();
Destination folder file path array
var $targetPaths = array ();
Size of the folder before compression encryption
var $sizeBeforeZip = null;
Size of the folder after compression encryption
var $sizeAfterZip = null;
The output of the interrupted line
var $newline = ';
/**
* Construction of the structure of the child
*
* @param string $sourceDir source folder
* @param string $targetDir Purpose folder
* @param boolean $bcompiler whether to encrypt
* @param boolean $strip whether to remove whitespace from the line
* @return Boolean
*/
Public Function phpcodezip ($sourceDir = '. ', $targetDir = ' tmp ', $bcompiler =true, $strip =true) {
Configuring initial variables
$this->sourcedir = $sourceDir;
$this->targetdir = $targetDir;
$this->bcompiler = $bcompiler;
Check to see if the source information exists
if (!is_dir ($this->sourcedir)) {
Die (' designated source folder '. $this->sourcedir. ' No, please re-set ');
} else {
If the specified destination folder is present, chop off the re-
if (Is_dir ($this->targetdir)) {
Echo ' Initialize destination folder '. $this->newline. $this->newline;
$this->cleandir ($this->targetdir,true);
}
To create a folder for the source folder
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);
}
Get the source folder file path checklist
$this->sourcefilepaths = $this->getpaths ($this->sourcedir, ' * ');
File path checklist with the destination
foreach ($this->sourcefilepaths as $key = + $path) {
Set Purpose folder file path
$path = explode ('/', $path);
$path [0] = $this->targetdir;
$this->targetpaths[$key] = join ('/', $path);
}
The folder size before recording
$this->sizebeforezip = $this->getsizeunit ($this->getdirsize ($this->sourcedir), 2);
Echo $this->newline. $this->newline;
}
}
/**
* In-line compression encryption
* @return Boolean
*/
Public Function zip () {
$this->newline = ";
Echo ' Starts the Encryption program ' (Folder size: '. $this->sizebeforezip. ') '. $this->newline. $this->newline;
The source file is compressed.
foreach ($this->sourcefilepaths as $key = + $path) {
if (Is_file ($path)) {
Get file Info
$pathInfo = PathInfo ($path);
Echo ' read from Source: '. $path. $this->newline;
Get the content after compression
echo ' = Remove blanks ...... ';
if ($this->strip && $pathInfo [' extension '] = = ' php ') {
$fileAterZip = Php_strip_whitespace ($path);
} else {
$fileAterZip = file_get_contents ($path);
}
Echo ' Finish '. $this->newline;
The content of the compression is written to the destination.
$fp = fopen ($this->targetpaths[$key], ' w+ ');
The echo ' = ' is written into the destination file ... ';
Fwrite ($fp, $FILEATERZIP);
Fclose ($FP);
Echo ' Finish '. $this->newline;
If you chose to encrypt
if ($this->bcompiler && $pathInfo [' extension '] = = ' php ') {
echo ' = Encrypt the original files ... ';
Duplicate the original file
$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);
Delete unencrypted original files
Unlink ($this->targetpaths[$key]);
Re-name the encrypted files.
Rename ($this->targetpaths[$key]. ' Encrypt.php ', $this->targetpaths[$key]);
Echo ' Finish '. $this->newline;
}
Echo $this->newline. $this->newline;
}
}
Re-calculate the folder size after compression encryption
$this->sizeafterzip = $this->getsizeunit ($this->getdirsize ($this->targetdir), 2);
Echo ' End cipher program '. $this->newline. $this->newline;
echo ' report info '. $this->newline;
Echo ' source folder: '. $this->sourcedir. ' ('. $this->sizebeforezip. ') '. $this->newline;
Echo ' Destination folder: '. $this->targetdir. ' ('. $this->sizeafterzip. ') '. $this->newline;
echo ' File size increase: + '. $this->getsizeunit ($this->getdirsize ($this->targetdir)-$this->getdirsize ($this- >sourcedir)). $this->newline;
echo ' Archives: '. Count ($this->sourcefilepaths). ' '. $this->newline;
}
/**
* Delete all files from the folder
*
* @param string $dir folders to delete
* @param boolean $deleteSelf delete folders at the same time
* @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);
}
}
/**
* The size of the file that gets the folder
*
* @param string $dir The folder you want to analyze
* @return int bit tuple
*/
Private Function getdirsize ($dir = '. ') {
Get File path Checklist
$filePaths = $this->getpaths ($dir, ' * ');
Initialize the digital calculator
$sizeCounter = 0;
foreach ($filePaths as $key = = $path) {
$sizeCounter = $sizeCounter + filesize ($path);
}
return ($sizeCounter);
}
/**
* Get all matching paths for the folder
*
* @param string $start _dir folder for analysis
* @return Array file path 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;
}
/**
* File size conversion function
*
* @param int File size
* @param int decimal digits
* @param Boolean if you want to cut the information into arrays
* @return Mix string or array
*/
Public Function Getsizeunit ($size, $decimal =2, $split =false) {
Set the sequence of single bits
$unit = Array (' Bytes ', ' KB ', ' MB ', ' GB ', ' TB ', ' PB ', ' EB ', ' ZB ', ' YB ');
Initializing indexes
$flag = 0;
To do a simple chemical control calculation.
while ($size >= 1024) {
$size = $size/1024;
$flag + +;
}
Do you want to poorly transposition the numbers with the simple
if ($split) {
$sizeUnit = Array (
' Size ' = Number_format ($size, $decimal),
' Unit ' = $unit [$flag]
);
} else {
$sizeUnit = (Number_format ($size, $decimal)). $unit [$flag];
}
return size and unit
return ($sizeUnit);
}
}
http://www.bkjia.com/PHPjc/322446.html www.bkjia.com true http://www.bkjia.com/PHPjc/322446.html techarticle Instructions for use://Load function include_once (' phpcodezip.php ');//Create an encrypted file (SourceDir the directory of PHP files to encrypt, targetdir the encrypted file directory) $encryption = new Phoco ...