


This article mainly introduces the method of using exec, system and other functions to call system commands in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
PHP AS A server-side scripting language, it is fully capable of tasks such as writing simple or complex dynamic web pages. But things are not always like this. Sometimes in order to implement a certain function, you must resort to external programs (or commands) of the operating system. In this way, you can get twice the result with half the effort.
php's built-in functions exec and system can be used Call system commands (shell commands), and of course passthru, escapeshellcmd and other functions.
In many cases, using PHP's exec, system and other functions to call system commands can help us complete our work better and faster. For example, exec helped me a lot when I was batch processing .rar files two days ago.
Today I will sort out the commonly used calling system functions and share my experience with everyone.
Note: If you want to use these two functions, the safe mode in php.ini must be turned off, otherwise PHP will not allow you to call system commands for security reasons.
First take a look at the explanation of these two functions in the PHP manual:
exec --- Execute external programs
Syntax: string exec ( string command [, array &output [, int &return_var]] )
Explanation:
exec() executes the given command command, but it does not output anything, it simply takes the command The last line is returned in the result. If you need to execute a command and get all the data from the command, you can use the passthru() function.
If the parameter array is given, the specified array will be filled with each line output by the command. Note: If the array already contains some elements, exec() will append it to the array. later, if you don't want this function to append elements, you can call unset() before passing the array to exec().
If the parameters array and return_var are given, the status command returned to execution will be written to this variable.
Note: If you allow data from user input to be passed to this function, then you should use escapeshellcmd() to ensure that the user cannot trick the system into executing arbitrary commands. .
Note: If you use this function to start a program and want to leave it while it is running in the background, you must make sure that the output of the program is redirected to a file or some other Output data stream, otherwise PHP will hang until the program execution ends.
system --- Execute external programs and display output
Syntax: string system (string command [, int &return_var])
Description:
system() executes the given command command and outputs the result. If the parameter return_var is given, the status code of the executed command will be written to this variable.
Note: If you allow data from user input to be passed to this function, then you should use escapeshellcmd() to ensure that the user cannot trick the system into executing arbitrary commands. .
Note: If you use this function to start a program and want to leave it while it is running in the background, you must make sure that the output of the program is redirected to a file or some other Output data stream, otherwise PHP will hang until the program execution ends.
If PHP is running as a server module, system() will try to automatically clear the web server's output buffer after outputting each line.
Returns the last line of the command if successful, false if failed.
If you need to execute a command and get all the data from the command, you can use the passthru() function.
Both of these are used to call system shell commands.
The difference is:
exec can return all execution results to the $output function (array), $status is the status of execution, 0 is success, 1 is failure
Systerm does not need to provide the $output function, it returns the result directly. Similarly, $return_var is the status code of execution, 0 is success, 1 is failure
exec example:
<?php $a = exec("dir",$out,$status); print_r($a); print_r($out); print_r($status); ?>
system example:
<?php $a = system("dir",$out); print_r($a); print_r($out); ?>
System, exce, passthru difference
system() Output and return the last line of shell results.
exec() does not output results and returns the last line of shell results. All results can be saved in a returned array.
passthru() only calls the command and outputs the command execution result directly to the standard output device as is.
Same points: you can get the status code of command execution
The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!
related suggestion:
Analysis of the filter_var() function and Filter function in PHP
Analysis on str_replace replacement vulnerability in php
The above is the detailed content of About how to use exec, system and other functions to call system commands in PHP. For more information, please follow other related articles on the PHP Chinese website!

php exec无法执行的解决办法:1、找到php配置文件php.ini,在文件里搜索“disable_functions”,并把“exec”和“shell_exec”去掉;2、到服务器修改文件权限;3、修改配置中的安全模式为“safe_mode=On”。

Java文档解读:System类的currentTimeMillis()方法用法解析,需要具体代码示例在Java编程中,System类是一个非常重要的类,其封装了与系统相关的一些属性和操作。其中,currentTimeMillis方法是System类中非常常用的一个方法,本文将对该方法做详细解读并提供代码示例。一.currentTimeMillis方法概述

win10系统是一款非常好用的高智能系统,强大的兼容性可以确保系统在正常的使用过程中基本不会出现任何的问题,但是随着人们对win10系统的不断使用有时候系统也会出现win10开机蓝屏终止代码systemserviceexception的问题,今天小编就为大家带来了win10开机蓝屏终止代码systemserviceexception的解决办法有需要的话就快来下载吧。win10systemserviceexception蓝屏的解决办法:方法一:1、使用Windows键+R打开运行,输入“contr

php exec结果乱码的解决办法:1、打开相应的PHP代码文件;2、查看exec处的代码;3、修改内容为“exec("python cmd.py",$str);foreach($str as $res){$str = iconv("GBK", "UTF-8", $res);}echo $str;”即可。

电脑的system是比较常见的一种系统进程,在查看进程的时候经常会看到system,这个进程简单来说就是电脑系统的意思;但是,如果电脑中出现了system.exe的进程,需要及时删除,这是一个木马病毒生成的文件,真正的system后边是没有exe后缀的。

功能描述exec命令在Linux中用于调用并执行指定的命令。通常在shell脚本中使用exec命令来执行其他命令。当在当前终端中使用exec命令时,指定的命令执行完毕后会取代当前进程,而不是创建一个新的子进程。命令语法exec[选项]选项含义选项含义-c使用一个空环境来执行-ashell会将名字作为零参数传递给执行的命令-l在shell的开头放置一个破折号参数传递给命令参考实例实例1//首先使用echo命令将文本“www.linuxyz.cn”进行输出:[root@bunian~]#echoww

微软已宣布System Center 2022 的可用性。最新版本带来了 System Center Operations Manager (SCOM)、Virtual Machine Manager (VMM)、System Center Orchestrator (SCORCH)、Service Manager (SM) 和 Data Protection Manager

时光倒流回到1990年代的Macintosh,在浏览器窗口中运行System7和MacOS8的完整虚拟安装。1990年代Mac软件的新虚拟版本存在一个缺陷,那就是它们以2020年代Mac的速度运行。您看到的是MacSE/30或Quadra700,但一切都与AppleSilicon一样快。您可以在这些模拟操作系统中进行实际工作,它们甚至可以将文档或文件从macOSMonterey拖入和拖出。但无论是出于某些实际目的还是更可能是为了纯粹的乐趣,这里是如何


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft