Home > Article > Backend Development > Use ActivePHP to build a version management system_PHP tutorial
When learning PHP, we are always taught that PHP is a server-side script and cannot be used to control the client. With the release of PHP5, this sentence is not so correct. Because now, PHP can also be used to write client-side scripts. Yes, you heard it right, write client scripts in PHP.
ActivePHP installation
Now we will demonstrate how to use PHP to write client scripts. First, you need to download the PHP5 installation package on Windows, and then unzip it to a directory, such as: C:Program FilesEasyPHP5php. Then, enter the command line mode of Windows, cd to the directory where you unzipped PHP5, and then type:
regsvr32 php5activescript.dll
After pressing Enter, you will see a success prompt:
This means you can already use it ActivePHP. Okay, let's write a simple script to test it. It is still a universal HelloWorld:P.
Code list?>
Save the above code as Hello.htm, then double-click it, you can see the result below.
Environmental requirements?>
PHP: 5.0.0
OS: Windows
Browser: IE
Well, the effect is good, but it is not client-side enough. Let's modify the code:
Code list?>
Run it again and see~
Do you feel something?
Our version management system
Let’s go back and talk about the version management system. The version management system we want to build is very simple. It is to package the files in the development directory and the data tables of the database into a RAR package, name it according to time and put it in a backup directory. Since the main purpose of this article is to demonstrate the use of ActivePHP, we will not consider the management of RAR packages and the content of decompressing them to overwrite the original data. However, for a version management system, this part is very important. We recommend that you Do it yourself ;).
Related knowledge ?>
Mysql database is stored in the mysql/data directory in the form of files, and one library corresponds to one directory.
First we need to know how PHP calls other programs on Windows, which is the System command. This command is as simple as Echo, just
System('command');
.
Then we need to know how to use the command line of RAR. For this kind of thing, of course you should find the help document, which is in the installation directory of RAR. After looking at the English pile for a long time, I finally found a method: write the file to be compressed into a text file, and then pass the file name as a parameter to RAR. Written as a command line:
rar.exe a path_to_save @file_list
Generating this file is very simple for PHP, just a traversal function, the following two functions are Improved from User Contribute in the PHP manual.
Code list?>
function R_walk($oldname, &$string)
{
if(is_file($oldname) )
{
$string .= $oldname ."rn";
}
else if(is_dir ( $oldname ) )
{
R_dir_walk($oldname, $string) ;
}
else
{
die("Cannot add file: $oldname (it's neither a file nor a directory)");
}
}
function R_dir_walk($oldname, &$string)
{
$dir = opendir( $oldname );
while( $file = readdir( $dir ) )
{
if ( $file == "."
$file == ".." )
{
continue;
}
R_walk("$oldname/$file", $string );
}
closedir($dir);
}
With these two functions, it is easy to generate a list file.
The following is the code for the actual operation:
Code list?>
$php_path = 'C:/Program Files/ EasyPHP1-7/home/dev/R4/';
$mysql_path = 'C:/Program Files/EasyPHP1-7/mysql/data/r4/';
$date = date( "Y_m_d_H_i_s " );
$bakeup_path = 'D:/bakeup/R4/Backup_'.$date;
// copy file
R_walk( $php_path , $files );
// stop mysql
$window->alert( 'Mysql service process is about to be shut down...' );
system( 'mysqladmin.exe -uroot shutdown' );
R_walk( $mysql_path , $ files );
$files = str_replace( '/' , '\' , $files );
write2_file( './info.txt' , $files );
$window->alert( 'Compression starts, please do not close the CMD window manually...' );
system( 'rar.exe a "' . $bakeup_path . '" @"./info.txt" ' );
$window->alert( 'Compression completed, Mysql will be restarted soon, please manually close the CMD window that pops up below...' );
// restart mysql
system( 'mysqld.exe&' );
The above code is very simple and only explains a few places
· Mysql will lock the data table when running , so we need to stop the service before compression and start it again after compression is completed.
· System command will wait for the command to be completed before continuing to execute. Mysqld.exe is a background service and will not stop, so the program will enter the waiting state here. Just close the CMD window manually. .
· The paths of programs such as rar and mysqld above are added to the environment variables, so there is no need to specify them. The place to add environment variables in Windows XP is: My Computer (right click/Properties) -> Advanced -> Environment Variables -> System Variables (Path).
Okay, add the above code, save it, and run it again: it’s very convenient, HoHo~ That’s it for the article, remember to finish your homework :P
( Source: Viphot)