PHP execute Linux command line small example--file compression, _php tutorial

Source: Internet
Author: User
Tags zip extension ziparchive

PHP executes the Linux command line applet--file compression,


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=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 related file after downloading 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 Exl_file directory   */   $outputs  =  array   ();  /*   execute linux command with PHP exec the 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; 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 !=0 ) {  foreach  ( $outputs   as   $ko  =     $vo  ) {  echo  " $vo  
" ; }} else { $zipfile = './exl_file/wow.zip ' ; // file download output after deleting related files }}


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

http://www.bkjia.com/PHPjc/1128381.html www.bkjia.com true http://www.bkjia.com/PHPjc/1128381.html techarticle PHP Implementation of the Linux command line small example-file compression, the first few days of work, need to be 3 txt files, packaged into *.zip down to the beginning of the local, I and ordinary youth, think of PHP built-in ...

  • 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.