run php in cmd code example
Example 1: how to execute cmd command in php
I'm not sure what shell you are going to get with this function, but you can find out like this:
".shell_exec($cmd)."
";
?>
On my FreeBSD 6.1 box I get this:
USER=root
LD_LIBRARY_PATH=/usr/local/lib/apache2:
HOME=/root
PS1='$ '
OPTIND=1
PS2='> '
LOGNAME=root
PPID=88057
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
SHELL=/bin/sh
IFS='
'
Very interesting. Note that the PATH may not be as complete as you need. I wanted to run Ghostscript via ImageMagik's "convert" and ended up having to add my path before running the command:
&1';
echo "
".shell_exec($cmd)."
";
?>
ALSO, note that shell_exec() does not grab STDERR, so use "2>&1" to redirect it to STDOUT and catch it.
Example 2: php run command line
shell_exec('ls');
// or
exec('ls');
Example 3: how to run php on windows
If php installed - in cmd > php filename.php
Follow the steps if php not installed, https://www.sitepoint.com/how-to-install-php-on-windows/
Example 4: execute php file from command line
php -f /path/to/your/file.php
Example 5: execute script php command line
php php_file.php
// or
php php_file.php > result_file.txt // to display the result of echoes or dumps