Instructions for use:
Load function
Include_once (' phpcodezip.php ');
Create the encrypted file (SourceDir the php file directory to encrypt, TARGETDIR the encrypted file directory)
$encryption = new Phocodezip (' SourceDir ', ' targetDir ');
Perform line encryption
$encryption->zip ();
phpcodezip.php Source Download
Phpcodezip.rar
phpcodezip.php Source Content
Copy Code code as follows:
/*
* @license: MIT & GPL
*/
Class phpcodezip{
For compressed, encrypted source folders.
var $sourceDir = '. ';
A compressed, encrypted, stored folder.
var $targetDir = ' tmp ';
Whether the encryption is done
var $bcompiler = true;
Do you want to remove blank registration?
var $strip = true;
Source Data folder path array
var $sourcefilePaths = array ();
Destination folder file path array
var $targetPaths = array ();
The size of the data folder before compressing the encryption
var $sizeBeforeZip = null;
The size of the data folder that is compressed and encrypted
var $sizeAfterZip = null;
The output of an interrupted line
var $newline = ';
/**
* Constructs the structure
*
* @param string $sourceDir source folder
* @param string $targetDir destination Folder
* @param Boolean $bcompiler whether encryption is done
* @param boolean $strip whether to remove blank registration lines
* @return Boolean
*/
Public Function phpcodezip ($sourceDir = '. ', $targetDir = ' tmp ', $bcompiler =true, $strip =true) {
Configure initial variables
$this->sourcedir = $sourceDir;
$this->targetdir = $targetDir;
$this->bcompiler = $bcompiler;
Check to see if the source data exists
if (!is_dir ($this->sourcedir)) {
Die (' specified source folder '. $this->sourcedir. ' does not exist, please reset ');
} else {
If the specified destination folder exists, the trainer is cut off
if (Is_dir ($this->targetdir)) {
Echo ' Initialize destination folder '. $this->newline. $this->newline;
$this->cleandir ($this->targetdir,true);
}
To create a destination folder for the source folder structure.
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's file path checklist
$this->sourcefilepaths = $this->getpaths ($this->sourcedir, ' * ');
Match the destination of the file path clear
foreach ($this->sourcefilepaths as $key => $path) {
Set the destination Data folder path
$path = explode ('/', $path);
$path [0] = $this->targetdir;
$this->targetpaths[$key] = join ('/', $path);
}
The size of the folder before recording
$this->sizebeforezip = $this->getsizeunit ($this->getdirsize ($this->sourcedir), 2);
Echo $this->newline. $this->newline;
}
}
/**
* Compressing encryption
* @return Boolean
*/
Public Function zip () {
$this->newline = ';
echo "Start the Encryption Program" (Folder Size: '. $this->sizebeforezip. ') '. $this->newline. $this->newline;
The source file will be compressed.
foreach ($this->sourcefilepaths as $key => $path) {
if (Is_file ($path)) {
Get file information
$pathInfo = PathInfo ($path);
Echo ' Read the source file: '. $path. $this->newline;
To obtain the compressed contents
Echo ' => to remove blank annotations ... ';;
if ($this->strip && $pathInfo [' extension '] = = ' php ') {
$fileAterZip = Php_strip_whitespace ($path);
} else {
$fileAterZip = file_get_contents ($path);
}
Echo ' Finish '. $this->newline;
The compressed content is written to the target position.
$fp = fopen ($this->targetpaths[$key], ' w+ ');
Echo ' => written in the destination ... '..
Fwrite ($fp, $FILEATERZIP);
Fclose ($FP);
Echo ' Finish '. $this->newline;
If you choose to encrypt the line
if ($this->bcompiler && $pathInfo [' extension '] = = ' php ') {
Echo ' => encrypt the original file ... ';
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 an unencrypted raw file
Unlink ($this->targetpaths[$key]);
Renaming the Encrypted file
Rename ($this->targetpaths[$key]. ' Encrypt.php ', $this->targetpaths[$key]);
Echo ' Finish '. $this->newline;
}
Echo $this->newline. $this->newline;
}
}
Recalculate the size of the compressed encryption folder
$this->sizeafterzip = $this->getsizeunit ($this->getdirsize ($this->targetdir), 2);
Echo ' "End Encryption 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 ' file: '. Count ($this->sourcefilepaths). ' A '. $this->newline;
}
/**
* Delete all files in the folder
*
* @param string $dir The folder you want to delete
* @param boolean $deleteSelf Delete data 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);
}
}
/**
* Get the file size of the folder
*
* @param string $dir The Data folder you want to dissect
* @return int bit Group
*/
Private Function getdirsize ($dir = '. ') {
Get a file path clearance
$filePaths = $this->getpaths ($dir, ' * ');
Initialization of the calculator
$sizeCounter = 0;
foreach ($filePaths as $key => $path) {
$sizeCounter = $sizeCounter + filesize ($path);
}
return ($sizeCounter);
}
/**
* Get all the matching paths of the data folder
*
* @param string $start the _dir Data folder
* @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 single switch function
*
* @param int File size
* @param int decimal bit
* @param boolean to cut data into arrays
* @return Mix string or array
*/
Public Function Getsizeunit ($size, $decimal =2, $split =false) {
Set up a single bit sequence
$unit = Array (' Bytes ', ' KB ', ' MB ', ' GB ', ' TB ', ' PB ', ' EB ', ' ZB ', ' YB ');
Initializing indexes
$flag = 0;
It's a simple chemical control.
while ($size >= 1024) {
$size = $size/1024;
$flag + +;
}
Do you want to open the numbers with a single non-transposition
if ($split) {
$sizeUnit = Array (
' Size ' => number_format ($size, $decimal),
' Unit ' => $unit [$flag]
);
} else {
$sizeUnit = (Number_format ($size, $decimal)). $unit [$flag];
}
Back-handed size and unit
return ($sizeUnit);
}
}