Table of Contents
PHP执行Linux命令行小例——文件压缩,
Home php教程 php手册 PHP执行Linux命令行小例文件压缩,

PHP执行Linux命令行小例文件压缩,

Jun 13, 2016 am 08:39 AM
linux php compression Command Line implement document

PHP执行Linux命令行小例——文件压缩,

      前几天工作中,需要将3个txt文件,打包成*.zip down到本地……

      一开始,我和普通青年一样,想到用PHP内置的 ZipArchive,代码看起来应该是这样的:

     

<span>/*</span><span>拆分成3个txt文件 分别是wow_1.txt wow_2.txt 和 wow_3.txt</span><span>*/</span>

<span>$zip</span>=<span>new</span><span> ZipArchive();
</span><span>$zipfile</span>='./Exl_file/wow.zip'<span>;
</span><span>if</span>(<span>$zip</span>->open(<span>$zipfile</span>,ZIPARCHIVE::CREATE)===<span>TRUE</span><span>){
    </span><span>$zip</span>->addFile('./Exl_file/wow_1.txt','wow_1.txt'<span>);
    </span><span>$zip</span>->addFile('./Exl_file/wow_2.txt','wow_2.txt'<span>);
    </span><span>$zip</span>->addFile('./Exl_file/wow_3.txt','wow_3.txt'<span>);
    </span><span>$zip</span>-><span>close();
        
    </span><span>//</span><span>下载  输出文件后删除相关文件</span>
<span>        
}</span><span>else</span><span>{
    </span><span>echo</span> "ZIP生成失败!"<span>;
}</span>
Copy after login

可是纠结的是正式环境上没安装zip扩展,ZipArchive直接没法用,代码绝对比在上面装个扩展要来的快——用PHP调用Linux的命令行 ,执行压缩命令,OK,马上行动!

<span>/*</span><span>拆分成3个txt文件 分别是wow_1.txt wow_2.txt 和 wow_3.txt  全部放到 Exl_file 目录下</span><span>*/</span>

<span>$outputs</span>=<span>array</span><span>();

</span><span>/*</span><span>用php的exec执行Linux命令   括号里的字符串就是你在Linux命令窗口里敲的命令;
   第二个参数是  linux执行该命令后返回的结果数组;
   linux执行返回的每一条结果依次存入该数组 
   
   第三个参数是结果,如果执行成功,则Linux返回结果值为0,如果执行失败,则结果值不为0
   
   </span><span>*/</span>
   

<span>exec</span>("zip ./Exl_file/wow.zip ./Exl_file/wow_1.txt ./Exl_file/wow_2.txt ./Exl_file/wow_3.txt",<span>$outputs</span>,<span>$rc</span><span>);


</span><span>if</span>(<span>$rc</span>!=0<span>){
    </span><span>foreach</span> (<span>$outputs</span> <span>as</span> <span>$ko</span>=><span>$vo</span><span>){
        </span><span>echo</span> "<span>$vo</span><br/>"<span>;
    }
}</span><span>else</span><span>{
    </span><span>$zipfile</span>='./Exl_file/wow.zip'<span>;
        
    </span><span>//</span><span>文件下载  输出后删除相关文件</span>
<span>    }
}</span>
Copy after login


你可以把 if($rc!=0) 改成 if(1==1) 查看Linux执行命令返回的结果行,如下图:

adding: Exl_file/wow_1.txt (deflated 96%<span>)
adding</span>: Exl_file/wow_2.txt (deflated 97%<span>)
adding</span>: Exl_file/wow_3.txt (deflated 97%)
Copy after login

    可以看到执行返回的信息全部输入到了 $outputs 数组中,*.zip文件生成成功。

    关于PHP执行Linux命令的相关文章网上搜索有很多,推荐一篇:http://blog.csdn.net/yangjun07167/article/details/5603425

     

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

PHP and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

What underlying technologies does Docker use? What underlying technologies does Docker use? Apr 15, 2025 am 07:09 AM

Docker uses container engines, mirror formats, storage drivers, network models, container orchestration tools, operating system virtualization, and container registry to support its containerization capabilities, providing lightweight, portable and automated application deployment and management.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles