PHP execute Linux command line small example--file compression

Source: Internet
Author: User
Tags zip extension ziparchive

In the first few days of work, 3 txt files need to be packaged into a *.zip down to local ...

In the beginning, like the normal youth, I thought of PHP built-in ziparchive, the code should look like this:

/*split into 3 txt files wow_1.txt wow_2.txt and wow_3.txt respectively*/$zip=Newziparchive ();$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 related files after downloading the output file        }Else{    Echo"Zip build Failed!" ";}

But the tangle is the formal environment does not install the zip extension, ziparchive directly can not be used, the code is absolutely more than on the above to install an extension to come fast-with PHP call Linux command line, execute the compression command, OK, immediately action!

/*split into 3 txt files wow_1.txt wow_2.txt and wow_3.txt are all placed in the Exl_file directory*/$outputs=Array();/*executing the linux command with the PHP exec string in parentheses is the command you hit in the Linux command window, and the second parameter is the array of results returned by Linux after executing the command; The third parameter to the array that the Linux execution returns is the result, If the execution succeeds, the Linux return result value is 0, and 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? =n){    foreach($outputs  as $ko=$vo){        Echo"$vo<br/> "; }}Else{    $zipfile= './exl_file/wow.zip '; //Delete related files after file download and output    }}


You can change if ($RC!=0) to if (1==1) to see the result rows returned by the Linux execution command, such as:

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 information returned by the execution is all entered into the $outputs array, and the *.zip file is generated successfully.

About PHP execute linux command articles online search has a lot of, recommend one article:http://blog.csdn.net/yangjun07167/article/details/5603425

PHP execute Linux command line small example--file compression

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.