How to execute cmd command in php

怪我咯
Release: 2023-03-13 19:22:02
Original
2946 people have browsed it

命令提示符是在操作系统中,提示进行命令输入的一种工作提示符。在不同的操作系统环境下,命令提示符各不相同。

windows环境下,命令行程序为cmd.exe,是一个32位的命令行程序,微软Windows系统基于Windows上的命令解释程序,类似于微软的DOS操作系统。输入一些命令,cmd.exe可以执行,比如输入shutdown -s就会在30秒后关机。总之,它非常有用。打开方法:开始-所有程序-附件 或 开始-寻找-输入:cmd/cmd.exe 回车。它也可以执行BAT文件。

这篇文章主要介绍了PHP中执行cmd命令的方法,需要的朋友可以参考下

本文介绍下,在php代码中执行cmd命令的方法,介绍下在php.ini文件中配置safe_mode参数支持命令执行的方法,有需要的朋友参考下。

说明:
本节内容在wamp包安装的环境实现。

首先,打开php.ini,关掉安全模式safe_mode = off,然后在看看 禁用函数列表 disable_functions = proc_open, popen, exec, system, shell_exec ,把exec去掉。
php代码:

<?php
exec("mkdir d:\\test",$out);
print_r($out);
?>
Copy after login

执行该php文件,会发现在d盘下多了一个test文件夹。

参考文档:

exec函数解析
exec语法: string exec(string command, string [array], int [return_var]);
exec返回值: 字符串

exec参数说明
Command – 需要执行的命令
Array – 是输出值
return_var –是返回值0或1,如果返回0则执行成功,返回1则执行失败。
exec不成功,调试方案

技巧分享:

使用管道命令, 使用 2>&1, 命令就会输出shell执行时的错误到$output变量, 输出该变量即可分析。

例如:

exec(‘convert a.jpg b.jpg&#39;, $output, $return_val);
Copy after login

修改为:

代码如下:

exec(‘convert a.jpg b.jpg 2>&1′, $output, $return_val);
print_r($output);
Copy after login

The above is the detailed content of How to execute cmd command in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!