A few days ago work, need to 3 TXT file, packaged into *.zip down to the local ...
In the beginning, like the average youth, I thought that with PHP's built-in ziparchive, the code would look like this:
Copy Code code as follows:
/* split into 3 txt files are 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 ();
Delete the related files after downloading the output file
}else{
Echo ' Zip build failed! ";
}
But the tangled is the formal environment does not install the zip extension, ziparchive directly can not use, the code is absolutely more than in the above to install an extension to come quickly-use PHP to invoke the Linux command line, execute compression command, OK, immediately action!
Copy Code code as follows:
/* split into 3 txt files are wow_1.txt wow_2.txt and wow_3.txt all placed under exl_file directory * *
$outputs =array ();
* * Use PHP exec to execute the Linux command in parentheses the string is the command you knock in the Linux command window;
The second parameter is the array of results that Linux returns after executing the command;
Each result returned by the Linux execution is stored in the array in turn
The third parameter is the result, and if the execution succeeds, the Linux return result value is 0, and if 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 related files after file download output
}
}
You can change the if ($RC!=0) to an if (1==1) to see the result line returned by the Linux execution command, as shown in the following figure:
Copy Code code 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%)
You can see that the execution of the returned information is entered into the $outputs array and the *.zip file is generated successfully.