PHP calls the Linux command line to execute the file compression command

Source: Internet
Author: User
Tags zip extension ziparchive
At the beginning, I thought of using PHP's built-in ZipArchive, just like a common young man, to use PHP to call the Linux command line and execute the compression command, instead of installing the zip extension in the environment, if you are interested, you may want to learn about it. I hope this article will help you with your work a few days ago. you need to package three txt files *. zip down to local ......
In the first place, I thought of ZipArchive built in PHP like a common young man. the code looks like this:

The code is as follows:


/* Split the file into three txt files: wow_1.txt wow_2.txt and wow_3.txt */
$ Zip = new ZipArchive ();
$ Zipfile = './Exl_file/wow.zip ';
If ($ zip-> open ($ zipfile, ZIPARCHIVE: CREATE) === TRUE ){
$ Zip-> addFile ('./Exl_file/wow_1.txt', 'wow_1.txt ');
$ Zip-> addFile ('./Exl_file/wow_2.txt', 'wow_2.txt ');
$ Zip-> addFile ('./Exl_file/wow_3.txt', 'wow_3.txt ');
$ Zip-> close ();
// Download the output file and delete it.
} Else {
Echo "ZIP generation failed! ";
}


However, the tangle is that zip extensions are not installed in the official environment, and ZipArchive cannot be used directly. the code is definitely faster than installing an extension above-using PHP to call the Linux command line, execute the compression command. OK, act now!

The code is as follows:


/* Split the file into three txt files. wow_1.txt wow_2.txt and wow_3.txt are all stored in the Exl_file directory */
$ Outputs = array ();
/* Execute the string in the brackets of the Linux command using php exec, which is the command you typed in the Linux command window;
The second parameter is the result array returned by linux after the command is executed;
Each result returned by linux execution is saved to this array in sequence.
The third parameter is the result. if the execution is successful, the result value returned by Linux is 0. if the execution fails, the result value is not 0.
*/
Exec ("zip./Exl_file/wow.zip./Exl_file/wow_1.txt./Exl_file/wow_2.txt./Exl_file/wow_3.txt", $ outputs, $ rc );
If ($ rc! = 0 ){
Foreach ($ outputs as $ ko => $ vo ){
Echo "$ vo
";
}
} Else {
$ Zipfile = './Exl_file/wow.zip ';
// Delete the relevant file after the file is downloaded and output
}
}


You can set if ($ rc! = 0) change to if (1 = 1) To view the result line returned by the Linux command, for example:

The code is as follows:


Adding: Exl_file/wow_1.txt (deflated 96%)
Adding: Exl_file/wow_2.txt (deflated 97%)
Adding: Exl_file/wow_3.txt (deflated 97%)


The returned information is all input to the $ outputs array. the *. zip file is generated successfully.

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.