Instructions for use:
// Load the Function
Include_once ('phpcodezip. php ');
// Create an encrypted file (the directory of the PHP file to be encrypted by sourceDir and the directory of the files encrypted by targetDir)
$ Encryption = new PhoCodeZip ('sourcedir', 'targetdir ');
// Execute row Encryption
$ Encryption-> zip ();
PhpCodeZip. php source code download
PhpCodeZip.rar
PhpCodeZip. php source code contentCopy codeThe Code is as follows :/*
* @ License: MIT & GPL
*/
Class PhpCodeZip {
// Source information to be encrypted
Var $ sourceDir = '.';
// Encrypt the stored data
Var $ targetDir = 'tmp ';
// Whether the data is encrypted
Var $ bcompiler = true;
// Whether to remove the blank trim to parse the rows
Var $ strip = true;
// Route records of the source data records
Var $ sourcefilePaths = array ();
// Destination information records
Var $ targetPaths = array ();
// Enter the data size before encryption
Var $ sizeBeforeZip = null;
// Encrypt the encrypted data size.
Var $ sizeAfterZip = null;
// Extract the output of the rows
Var $ newline = '';
/**
* Create a constructor
*
* @ Param string $ sourceDir origin Resource
* @ Param string $ targetDir target resource
* @ Param boolean $ whether bcompiler is encrypted
* @ Param boolean $ whether strip removes the blank limit to parse the limit row
* @ Return boolean
*/
Public function PhpCodeZip ($ sourceDir = '.', $ targetDir = 'tmp ', $ bcompiler = true, $ strip = true ){
// Configure the initial changes
$ This-> sourceDir = $ sourceDir;
$ This-> targetDir = $ targetDir;
$ This-> bcompiler = $ bcompiler;
// Check whether the source data exists
If (! Is_dir ($ this-> sourceDir )){
Die ('specifies the source resource quota'. $ this-> sourceDir. 'does not exist. Please reset it ');
} Else {
// If the specified destination resource exists, cut down the duplicate shard.
If (is_dir ($ this-> targetDir )){
Echo '[initialize destination information]'. $ this-> newline. $ this-> newline;
$ This-> cleanDir ($ this-> targetDir, true );
}
// Establish the same object information as the source data 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 );
}
// Retrieve the inventory statement of the source data
$ This-> sourcefilePaths = $ this-> getPaths ($ this-> sourceDir ,'*');
// Configure the checklist for the corresponding destination
Foreach ($ this-> sourcefilePaths as $ key => $ path ){
// Set the target data warehouse case
$ Path = explode ('/', $ path );
$ Path [0] = $ this-> targetDir;
$ This-> targetPaths [$ key] = join ('/', $ path );
}
// Record the data size before the row
$ This-> sizeBeforeZip = $ this-> getSizeUnit ($ this-> getDirSize ($ this-> sourceDir), 2 );
Echo $ this-> newline. $ this-> newline;
}
}
/**
* Perform encryption.
* @ Return boolean
*/
Public function zip (){
$ This-> newline = '';
Echo '[initial encryption program] (item size:'. $ this-> sizeBeforeZip. ')'. $ this-> newline. $ this-> newline;
// Prepare the source region in the future
Foreach ($ this-> sourcefilePaths as $ key => $ path ){
If (is_file ($ path )){
// Obtain the case information
$ PathInfo = pathInfo ($ path );
Echo 'fetch source response: '. $ path. $ this-> newline;
// Obtain the content after the specified volume
Echo '=> remove blank solution ..........';
If ($ this-> strip & $ pathInfo ['extension'] = 'php '){
$ FileAterZip = php_strip_whitespace ($ path );
} Else {
$ FileAterZip = file_get_contents ($ path );
}
Echo 'finished line'. $ this-> newline;
// Retrieve the internal volume after the upload to the destination location
$ Fp = fopen ($ this-> targetPaths [$ key], 'W + ');
Echo '=> login into destination ..........';
Fwrite ($ fp, $ fileAterZip );
Fclose ($ fp );
Echo 'finished line'. $ this-> newline;
// If you select Encryption
If ($ this-> bcompiler & $ pathInfo ['extension'] = 'php '){
Echo '=> encrypt the original token ..........';
// Original shard
$ Fh = fopen ($ this-> targetPaths [$ key]. 'encryption. php', "w ");
Bcompiler_write_header ($ fh );
Bcompiler_write_file ($ fh, $ this-> targetPaths [$ key]);
Bcompiler_write_footer ($ fh );
Fclose ($ fh );
// Remove unencrypted original tokens
Unlink ($ this-> targetPaths [$ key]);
// Rename the encrypted case
Rename ($ this-> targetPaths [$ key]. 'encryption. php', $ this-> targetPaths [$ key]);
Echo 'finished line'. $ this-> newline;
}
Echo $ this-> newline. $ this-> newline;
}
}
// Calculate the size of the encrypted data volume.
$ This-> sizeAfterZip = $ this-> getSizeUnit ($ this-> getDirSize ($ this-> targetDir), 2 );
Echo '[End encryption program]'. $ this-> newline. $ this-> newline;
Echo 'website advertising information'. $ this-> newline;
Echo 'source Information Sources: '. $ this-> sourceDir.' ('. $ this-> sizeBeforeZip.') '. $ this-> newline;
Echo 'destination information: '. $ this-> targetDir.' ('. $ this-> sizeAfterZip.') '. $ this-> newline;
Echo 'growth in case size: + '. $ this-> getSizeUnit ($ this-> getDirSize ($ this-> targetDir)-$ this-> getDirSize ($ this-> sourceDir ))). $ this-> newline;
Echo 'summary: '. count ($ this-> sourcefilePaths).'. $ this-> newline;
}
/**
* Excluding all cases
*
* @ Param string $ information to be deleted by dir
* @ Param boolean $ deleteSelf: Same-time data Addition
* @ 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 total size of data records obtained
*
* @ Param string $ information to be analyzed by dir
* @ Return int-bit metabase
*/
Private function getDirSize ($ dir = '.'){
// Obtain the statement.
$ FilePaths = $ this-> getPaths ($ dir ,'*');
// Initialize the Calculator
$ SizeCounter = 0;
Foreach ($ filePaths as $ key => $ path ){
$ SizeCounter = $ sizeCounter + filesize ($ path );
}
Return ($ sizeCounter );
}
/**
* Obtain all matching route entries of the resource.
*
* @ Param string $ start_dir information to be analyzed
* @ Return array route condition column
*/
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;
}
/**
* Statement of batch case size (single digit)
*
* @ Param int refers to the case size
* @ Param int small data point
* @ Param boolean: whether to cut the data into bytes Columns
* @ Return mix string or character Column
*/
Public function getSizeUnit ($ size, $ decimal = 2, $ split = false ){
// Set the single-bit sequence
$ Unit = array ('bytes ', 'kb', 'mb', 'gb', 'tb', 'petab', 'EB', 'zb ', 'yb ');
// Initialize the index
$ Flag = 0;
// Perform computation
While ($ size> = 1024 ){
$ Size = $ size/1024;
$ Flag ++;
}
// Whether to separate the data value from the single digit
If ($ split ){
$ SizeUnit = array (
'SIZE' => number_format ($ size, $ decimal ),
'UNIT '=> $ unit [$ flag]
);
} Else {
$ SizeUnit = (number_format ($ size, $ decimal). $ unit [$ flag];
}
// Response size and single bit
Return ($ sizeUnit );
}
}