popen() - 打开进程文件指针 - php 文件目录函数
popen()
(PHP 4, PHP 5, PHP 7)
打开进程文件指针
说明
popen(string $command,string $mode): resource打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。
参数
$command命令。
$mode模式。
返回值
返回一个和fopen()所返回的相同的文件指针,只不过它是单向的(只能用于读或写)并且必须用pclose()来关闭。此指针可以用于fgets(),fgetss()和fwrite()。当模式为'r',返回的文件指针等于命令的 STDOUT,当模式为'w',返回的文件指针等于命令的 STDIN。
如果出错返回FALSE
。
范例
Example #1popen()例子
如果未找到要执行的命令,会返回一个合法的资源。这看上去很怪,但有道理。它允许访问 shell 返回的任何错误信息:
Example #2popen()例子
If, on windows, you need to start a batch file that needs administrator privileges, then you can make a shortcut to the batch file, click properties, check to on "run as administrator" on one of the property pages, and then double-click the shortcut once (to initialize that "run as administrator" business). using popen("/path/to/shortcut.lnk") will then run your batch file with administrator privileges. handy for when you want to use cli php to do some long running tasks and that php-cli needs to use sessions..
Don't expect this function to return false when the executable doesn't exist in the first place. A stream will be opened anyway but nothing can be read from it. An error similar to "sh: 1: asdfasdfasdf: not found" will be printed to STDERR. Solution 1: Look at the return value of pclose(), it will be the exit status of the shell that runs the command. On Linux it will be 127 if the executable wasn't found. Otherwise it's the exit status of the executable itself. Solution 2: Use proc_open() instead, which allows to also capture STDERR and then parse it for errors. You probably should do both.
As a side note to the code provided by anonymous at anon dot com: $cmd = "php longscript.php"; function execInBackground($cmd) { if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); } else { exec($cmd . " > /dev/null &"); } } I had a problem where Windows would close the call too fast before the entire script was interpreted, but I didn't want my main script to hang until it would be fully loaded. As a workaround, I called a tiny .php script which would then call the larger script. myfile.php: timewrapper.php: This way my main script would continue to run without having to pause, while the tiny script pauses while it loads the larger file.
Note, when using this with a batch file in windows, you must put an "exit" at the end of your batch file or you will get a new cmd.exe stuck in your process list every time you execute the page.
Truncated output from ps command? The solution lies in the way ps displays it's info specifically the -w option which: 'uses 132 columns to display information, instead of the default which is your window size.'.... somehow with fgets in php that results in 74 characters regardless off the init length parameter a bit of code: Ciao, Rene ==
If you want to fork a process under windows, this is the function to use. I created a batch file called runcmd.bat with the following line start %1 %2 %3 %4 then I have the folowing function with this, doing something like will launch php.exe outside of apache and allow the script calling the runCmd() function to continue without waiting for the command line process to return. The process will run under the same user account that Apache (or whatever webserver you're running) is running under, so make sure it has permissions to do whatever you need to do. Also, make sure that the batch file has enough %n s in order to pass all the command line variables that you might need to pass. Special thanks to kicken from the devshed forums for coming up with the idea.
If you want to download files from a linux server with a filesize bigger than 2GB you can use the following:
If you are running in a chroot'ed environment on Debian "Squeeze", this command won't work; there is a problem with the kernel code that popen() eventually calls. Note that pecl makes heavy use of this command, so if you are running in this environment you will need to install the pecl extension from source instead.
There is a simple way to start a process in the background but still find out what the process result is. I combined the information from some users below with some of my own coming up with the following: In my case the file names of the .bat and .log files weren't always the same, so I needed a dynamic way to create the .bat file. The output from the php command is saved to the log file with the >> command. All prints and errors are stored there. At a later time you can open the log file and see what happened.
Another workaround for using popen() with "w" mode so that the stdout of the command reaches the browser: An easy solution is to have two php scripts; "real.php" with the popen($cmd, "w") command in it, the other being "wrapper.php", a one liner that simply invokes system("php real.php"); Invoking "wrapper.php" from the browser allows the popen($cmd,"w") in "real.php" to work as expected, such that stdout of $cmd reaches the browser. If you try to skip the wrapper and just run "real.php", stdout of $cmd is lost to /dev/null.
From the popen linux programmers manual: "The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag." Since php uses this popen function, you need to be sure /bin/sh exists. This file may not exist in chroot()ed environments.
The below code works for both way processing ;) Have fun folks
I had all kinds of trouble encrypting a message with PGP, but I finanlly got it to work. The trick was to 'chmod o+r pubring.pkr' so that the apache server could read the public keys!!! Then, this function worked fine:
I should say, my host uses a modified form of safe mode, so I don't know if that might have caused a problem with "popen" as opposed to "proc_open". With safe mode enabled, all words following the initial command string are treated as a single argument. Thus, echo y | echo x becomes echo "y | echo x". [Because of this,] LinixDude010's srcipt did not work for me. Seems wrong to read and write with popen, according to the manual. The script produced pgp text, but there was something wrong with the text and I could not decode it. This replacement script, using proc_open, which can read and write, DOES work:
Here is a nice little script for monitoring your http access log. ---- www.eviltree.co.uk www.solidsites.co.uk www.mongbong.com
I noticed that some of the examples above seem to advocate passing unencrypted data to gpg via the pipe shell escape, in the absence of a bi-directional popen (on some OSes). The approach I've taken is similar to:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)