Build a version management system with ActivePHP

Source: Internet
Author: User


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 scripts. Yes, you have not heard the error. Use PHP to write the client script.



ActivePHP Installation



Next we will demonstrate how to use PHP to write client scripts. First, you need to download the PHP5 installation package on windows and decompress it to a directory, for example, C: \ Program Files \ EasyPHP5 \ php. Next, go to the command line mode of windows, cd to the directory where you extract PHP5, and then type:

Regsvr32 php5activescript. dll


After you press enter, you will see a success prompt:




This shows that you can use ActivePHP. Well, let's write a simple script to test it. It's still the global HelloWorld: P.



<? Code List?>

<Script language = "ActivePHP5">
$ Document-> write ('Hello World! ');
</Script>




Save the code on the top to hello.htm, and double-click it to see the following result.



<? Environment requirements?>

PHP: 5.0.0

OS: Windows

Browser: IE





Well, the effect is good, but it is not characteristic of the client. Let's modify the code:



<? Code List?>

<Script language = "ActivePHP5">
$ Window-> alert ('Hello World! ');
</Script>




Run the command again ~




Is there a feeling?



Our Version Management System



Let's look back at the version management system. The version management system we want to do is very simple. It is to pack the files and database data tables in the Development Directory into a RAR package, name it by time, and put it in a backup directory. Because the main purpose of this article is to demonstrate the use of ActivePHP, we will not consider the management of RAR packages and extract the original data content, but for a version management system, this part is very important. We suggest you do it yourself ;).



<? Related Knowledge?>

Mysql databases are stored in the mysql/data directory as files. A database corresponds to a directory.




First, we need to know how PHP calls other programs on Windows, that is, the System command. This command is as simple as Echo, directly

System ('command ');


You can.



Then we need to know how to use the RAR command line. Of course we need to find the help documentation for this kind of things, just in the RAR installation directory. After reading it for a long time in the English heap, 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. The command line is:

Rar.exe a path_to_save @ file_list


Generating this file is very easy for PHP. A traversal function is enough. The following two functions are improved from the 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 code for the actual operation is as follows:



<? 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 ('closing the Mysql service process ...... ');
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 is complete and Mysql will be restarted soon. Please close the CMD window that appears below manually ...... ');

// Restart mysql
System ('mysqld.exe &');




The above code is very simple, just a few points

· Mysql locks the data table at runtime, so we need to stop the service before compression, and then start after compression.

· The systemcommand will be executed following the completion of the waiting command. mysqld.exe is a background service and will not be stopped. So the program enters the waiting state and closes the CMD window manually.

· The paths of the above rar and mysqld programs are added to the environment variables, so you do not need to specify them. For Windows XP, you can add environment variables in: My computer (right-click/attribute)-> advanced-> environment variable-> system variable (Path.



Okay, add the above Code, save the disk, and run it again. It's very convenient, HoHo ~ The article is here. Remember to finish your homework: P
(Source: Viphot)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.