PHP执行Linux命令行小例文件压缩,
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>
可是纠结的是正式环境上没安装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>
你可以把 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%)
可以看到执行返回的信息全部输入到了 $outputs 数组中,*.zip文件生成成功。
关于PHP执行Linux命令的相关文章网上搜索有很多,推荐一篇:http://blog.csdn.net/yangjun07167/article/details/5603425

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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)

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.

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.

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.
