At work a few days ago, I needed to package 3 txt files into *.zip and download them locally...
At first, like ordinary young people, I thought of using PHP’s built-in ZipArchive. The code should look like this:
Copy code The code is as follows:
/*Split into 3 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() ;
//Delete the relevant files after downloading the output file
}else{
echo "ZIP generation failed!";
}
But the problem is the formal environment Without the zip extension installed, ZipArchive cannot be used directly. The code is definitely faster than installing an extension on it - use PHP to call the Linux command line, execute the compression command, OK, take action now!
Copy code The code is as follows:
/*Split into 3 txt files: wow_1.txt wow_2.txt and Put all wow_3.txt in the Exl_file directory*/
$outputs=array();
/*Use php's exec to execute the Linux command. The string in the brackets is the command you type in the Linux command window;
The second parameter is the result array returned by Linux after executing the command;
Each result returned by Linux execution is stored in the array in turn
The third parameter is the result. If the execution is successful, Linux returns the result The value 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< br/>";
}
}else{
$zipfile='./Exl_file/wow.zip';
//Delete the relevant files after downloading and outputting the file
}
}
You can change if($rc!=0) to if(1==1) to view the result line returned by the Linux execution command, as shown below:
Copy code The code is 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 all the information returned by the execution is entered into the $outputs array, and the *.zip file is generated successfully.
http://www.bkjia.com/PHPjc/326526.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326526.htmlTechArticleWhile working a few days ago, I needed to package 3 txt files into *.zip and download them locally... 1 At first, like ordinary young people, I thought of using PHP’s built-in ZipArchive. The code should look like this...