[PHP 4, PHP 5, PHP 7, PHP 8]
popen — Opens process file pointer
Description
popen[string $command
, string $mode
]: resource|false
Parameters
command
The command
mode
The mode. Either 'r'
for reading, or 'w'
for writing.
On Windows, popen[] defaults to text mode, i.e. any \n
characters written to or read from the pipe will be translated to \r\n
. If this is not desired, binary mode can be enforced by setting mode
to 'rb'
and 'wb'
, respectively.
Return Values
Returns a file pointer identical to that returned by fopen[], except that it is unidirectional [may only be used for reading or writing] and must be closed with pclose[]. This pointer may be used with fgets[], fgetss[], and fwrite[]. When the mode is 'r', the returned file pointer equals to the STDOUT of the command, when the mode is 'w', the returned file pointer equals to the STDIN of the command.
If an error
occurs, returns false
.
Examples
Example #1 popen[] example
If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell:
Example #2 popen[] example
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.
anonymous at anon dot com ¶
11 years ago
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..
jlh ¶
6 years ago
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.
rjl at xs4all dot nl ¶
16 years ago
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 ==
Marbug at gmail dot com ¶
13 years ago
If you want to download files from a linux server with a filesize bigger than 2GB you can use the following:
cyberlot at cyberlot dot net ¶
20 years ago
The below code works for both way processing ;] Have fun folks
hacklor [AT] NOSPAM [DOT] com ¶
12 years ago
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.
don at digithink dot com ¶
16 years ago
erco at seriss dot com ¶
3 years ago
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.
antman3351 ¶
11 months ago
Note for Windows users using popen with start to run an external script without having php wait.
e.g.:
pclose[ popen[ 'start /b php someLongScript.php *> nul', 'rb' ] ];
If start can't find the exe it will open a popup message and pclose hangs until the popup is closed.
mrjake2 ¶
11 years ago
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.
atampone at NOSPAMFORME dot trdsupra dot com ¶
17 years ago
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
Special thanks to kicken from the devshed forums for coming up with the idea.
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.
linuxdude010 at yahoo dot com ¶
20 years ago
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:
//vmlinuz.nl/about/contact/ ¶
19 years ago
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.
PGP Dude ¶
17 years ago
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:
Cride5 ¶
16 years ago
Here is a nice little script for monitoring your http access log.
----
www.eviltree.co.uk
www.solidsites.co.uk
www.mongbong.com
ajv-php at erkle dot org ¶
20 years ago
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: