PHP executes the Linux command line example-file compression ,. PHP executes the Linux command line example-file compression. three txt files need to be packaged into * when working a few days ago *. zipdown to a local machine at the beginning, I think of using PHP built-in PHP to execute Linux command line example-file compression,
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:
/* 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 the file.} an error occurred while generating else {echo "ZIP! ";}
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!
/* Split the file into three txt files, and put all wow_1.txt wow_2.txt and wow_3.txt 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 after the linux command is executed; each result returned by linux execution is saved to the third parameter of the array in sequence. if the execution is successful, the return value of 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:
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.
About PHP to execute Linux command related articles online search there are a lot of, recommended one: http://blog.csdn.net/yangjun07167/article/details/5603425
Prepare, working a few days ago, you need to package three txt files into *. zip down to a local one. like normal youth, I want to use PHP built-in...