PHP calls Linux command line to execute file compression command _php tips

Source: Internet
Author: User
Tags zip extension ziparchive
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.

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.