PHP借用$cmd运行程序后,如何关闭?
我想用PHP运行某exe程序(不要CRON等计划程序,PHP里还有其他内容。这里方便测试,以记事本为例),在其打开3秒后,执行关闭。
如何操作?
是否可以调用任务管理器将其关闭?求解决代码。谢谢。
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><?php $cmd = 'C:\WINDOWS\system32\notepad.exe'; system($cmd); sleep(3); //$cmd1 = 'C:\WINDOWS\system32\taskmgr_original.exe'; //system($cmd1); ?>
------解决方案--------------------
- PHP code
<?php $handle = popen('C:\WINDOWS\system32\notepad.exe', "r"); pclose($handle); ?> <br><font color="#e78608">------解决方案--------------------</font><br>popen ― 打开进程文件指针<br><br>说明<br>resource popen ( string $command , string $mode )<br>打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。 <br><br>返回一个和 fopen() 所返回的相同的文件指针,只不过它是单向的(只能用于读或写)并且必须用 pclose() 来关闭。此指针可以用于 fgets(),fgetss() 和 fwrite()。 <br><br>如果出错返回 FALSE。 <br><br>Note: <br><br>如果需要双向支持,使用 proc_open()。 <br><br><br><br>Example #1 popen() 例子<br><br><?php <br />$handle = popen("/bin/ls", "r");<br>?> <br><br>Note: <br><br>如果未找到要执行的命令,会返回一个合法的资源。这看上去很怪,但有道理。它允许访问 shell 返回的任何错误信息: <br><br><?php <br />error_reporting(E_ALL);<br><br>/* 加入重定向以得到标准错误输出 stderr。 */<br>$handle = popen('/path/to/spooge 2>&1', 'r');<br>echo "'$handle'; " . gettype($handle) . "\n";<br>$read = fread($handle, 2096);<br>echo $read;<br>pclose($handle);<br>?> <br><br><br> <br><font color="#e78608">------解决方案--------------------</font><br>
------解决方案--------------------
popen返回的是notepad.exe在php环境下的进程指针,这个指针只能用于读取和输出数据给notepad,pclose关闭的只是这个指针而不是notepad本身.
我也不清楚如何关闭notepad,但是我想应该从windows的编程基础中查找如何获取任务管理器中的进程,然后用system函数去调用windows的关闭程序
------解决方案--------------------
popen是单向的,用proc_open吧
试试看
- PHP code
/** * windows only */ $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w") ); $cwd = 'C:\WINDOWS\system32'; $process = proc_open('notepad.exe', $descriptorspec, $pipes, $cwd); $s = proc_get_status( $process );//得到的信息都是父进程cmd.exe的状态,而非子进程notepad.exe的.所以不能直接kill掉这个process id sleep( 3 ); exec('taskkill /PID '.$s['pid'] . ' /T'); //树型删除,删除所有父进程与对应的子进程.原来以为子进程id必定大于父进程,写了一通代码,后来发现不是,且找到了这个命令 proc_close( $process ); <div class="clear"> </div>

在 Mac 上安装 Notepad++ 的步骤:下载 DMG 文件:从官方网站下载最新的 DMG 文件。安装 DMG 文件:打开 DMG 文件并将 Notepad++ 图标拖到“应用程序”文件夹中。启动 Notepad++:从“应用程序”文件夹中启动程序。授予完全磁盘访问权限(可选):如果提示,请授予 Notepad++ 完全磁盘访问权限。自定义设置(可选):通过“首选项”菜单调整设置以符合个人喜好。

要去除 Notepad 中的换行符,可以按照以下步骤操作:打开 Notepad。打开要编辑的文件。查找并替换换行符。单击“替换全部”。保存文件。

可使用三种方法在 Notepad 中实现文本列对齐:1、使用制表符;2、使用空格并手动调整;3、使用第三方工具(如 Notepad++、Sublime Text)提供自动对齐功能。

Notepad++乱码问题可以通过以下步骤解决:检查编码是否匹配文件内容转换文本格式为UTF-8或ANSI安装“编码转换”插件并尝试不同编码选项手动更改编码声明行(如果文件存在)重新启动Notepad++

如何使用换行符替换记事本中的文本:打开“查找和替换”对话框(编辑 > 查找和替换)。输入要查找的文本和要替换的换行符。选择换行符类型:“文本”、“Unicode”或“十六进制”。输入换行符的 Unicode 或十六进制代码。单击“全部替换”按钮以替换所有匹配文本。

在 Notepad 中替换换行符的方法:使用查找和替换功能,将当前换行符替换为所需的换行符。使用 Notepad++ 等高级文本编辑器,使用正则表达式进行更复杂的替换。使用 sed 命令行工具,直接编辑文件并替换换行符。

在 Notepad++ 中删除换行符的方法:1. 打开“编辑”菜单;2. 选择“替换”;3. 在“查找”字段中输入 \n;4. 在“替换为”字段中留空;5. 选中“替换全部”按钮。注意:操作前备份文件,删除操作无法撤消。


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

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
