PHP calls the Linux Command Line to execute the File compression command

Source: Internet
Author: User
Tags ziparchive

During work a few days ago, You Need To package three txt files into *. 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:
Copy codeThe 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!
Copy codeThe 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 <br/> ";
}
} 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:
Copy codeThe 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.