I'm trying to execute a .exe file form PHP.
I've set the PATH variable and confirmed that it works by running my command from random directory.
PHP code:
$command = "myCommand >> log.txt 2>&1";
echo exec($command, $output, $return)."<br>";
I've inspected the log.txt and it contains an error:
'myCommand' is not recognized as an internal or external command,
operable program or batch file.
What could be the reason for this issue? it seems that PHP won't look into all directories specified inside of PATH. What do you recommend that I do?
Specifying full path to myCommand works, but I'd like to omit it. I am running Windows 10.
Related
Hello i was trying to use exec() in php via web page to execute a file in the root directory, and for some reason none of my commands work except
ls
witch even when i do
ls /root
it doesn't work seems i can only do ls in the current directory or commands like whoami which returns apache
i've tried setting user permissions for apache, and i've tried setting permissions for file or /var/www/html directory and nothing seems to work any ideas?, my basic code below
<?php
command = "ls /root";
exec($command);
?>
The exec command return only the last line of your command.
Like said in the documentation, you should add a second parameter to get the result or use the function passthru
I have a C program that I wrote called convert3to5, originally written for CentOS / Fedora 32bit system in early 2010. I am moving it to new CentOS 6.x 64bit system host.
From a CentOS Putty console I can run the convert3to5 command just fine; here is a sample of it running from my console:
[root#cloud convert3to5]# ls
CircleStar convert3to5 Convert3To5.txt test.tif
[root#cloud convert3to5]# ./convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
TIFFReadDirectory: Warning, /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif: wrong data type 7 for "RichTIFFIPTC"; tag ignored. Image has an undefined fillorder - using default: MSB2LSB
The above is a normal completion of convert3to5 and I get a SV-DIS160217B.bmp that is placed in /var/www/webadmin/data/www/mydomain.com/uploads/ So running it from console works fine.
Question - I am attempting to run the same exact command from PHP using the exec(command, output, return) command as follows:
chdir($sv_path.$c3to5_path); //change our working directory to "/convert3to5" directory
$command = "./convert3to5 $targetFile 2>&1";
$result = exec($command, $output, $return);
// the output of the above command - is a .bmp file it will be placed in the same path as the input .tif file
I get the following $result:
ERROR: Unable to convert
/var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif to 5
color BMP file: Open file Error: Tiff_3_to_BMP_5_.lut!
My convert3to5 does need to open Tiff_3_to_BMP_5_.lut
Why does it find Tiff_3_to_BMP_5_.lut when I run convert3to5 from a console prompt but not from PHP exec(...) in both cases my pwd shows that I am in
[root#cloud convert3to5]# pwd
/var/www/webadmin/data/www/mydomain.com/myView/convert3to5
I have also verified pwd is correct from my PHP script after the
chdir($sv_path.$c3to5_path);
Tiff_3_to_BMP_5_.lut is in CircleStar directory - the path to CircleStar is /var/www/webadmin/data/www/mydomain.com/myView/convert3to5/CircleStar
Summary: ./convert3to5 works while PHP exec('convert3to5 ..) does not appear to work.
Can anyone suggest the difference and how to fix and/or debug?
Thanks
You're running the console from the convert3to5 directory, and I suspect your old C program used a relative path to the .lut file, possible relative to the .tif?
What if in the console example you did
cd ../..
./path/to/convert3to5/convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
Might be related to $targetFile. Print that and see if it's the full path.
Finally, run
/full/path/to/convert3to5 fullTargetPath
If that works, then as a workaround, if you just do exec('/full/path/to/convert3to5 $fullTargetPath, ..) it should behave like the console.
Per my above comment to wonton:
From the console I was running as root (so fully privileged). I supposed my PHP script will run as the "apache" user on the server?
Here was the problem I believe: I looked at the CircleStar directory privileges where the Tiff_3_to_BMP_5_.lut file exists. CircleStar had rw-r--r-- (0644) when running as root from console this allowed my convert3to5 program to find and open Tiff_3_to_BMP_5_.lut file just fine. However not the PHP exec(...) once I changed the privilege on CircleStar to rwxr-xr-x (0755) PHP exec(...) ran fine!
So ultimately it was a permission issue.
I want to run the simple embedded webserver from php via command line
php -S 0.0.0.0:8000
and let it show the content of the current directory.
As of the man page the only possible additional option to -S is -t for the document root. I know I can just create a index.php file with the following content
<html>
<body>
<?php
$directory = "./";
$files = glob($directory . "*");
foreach($files as $file) {
echo "".basename($file)."<br>";
}
?>
</body>
</html>
but I don’t want to create a file in that directory.
Isn’t it possible to run that code as a command line argument? I have tried it with the option -r and even with a virtual bash file and the -F-option as follows: -F <(… php code here …) but it seems true that the -S command only accepts -t as additional command.
Is there any trick to achieve it?
PS: I know that with python 2 or python 3 it is easily possible to show the current directory with python’s embedded webserver with
python -m SimpleHTTPServer # python 2
python -m http.server # python 3
but I want to do it with php.
Replace "./" from the assignment of variable $directory with the complete path of the directory you want to serve. Or, even better, change it to:
$directory = $_SERVER['DOCUMENT_ROOT'].'/';
to serve the path you provide as argument to -t in the command line.
Put index.php wherever you want in the file system and add its location (with complete path) to the end of the php command line.
If, for example, you save it as /home/erik/index.php, start PHP as:
php -S 0.0.0.0:8000 -t /home/erik/web /home/erik/index.php
PHP will use the script as a router. It will run it on every request and you can change it to interpret the request ($_GET[], $_SERVER[]) and generate different output, depending of the requested path and query string.
I have a series of shell commands I want to put in a program and execute the program from the command line. I decided to use PHP for this so currently I am trying to get the most basic shell commands to run.
Save as build.php
<?php
shell_exec('cd ..');
echo "php executed\n";
?>
From the command-line
php build.php
Output
php executed
Php executes correctly but I'm still in the same directory. How do I get shell_exec( ... ) to successfully call a shell command?
You need to change the cwd (current working directory) in PHP... any cd command you execute via exec() and its sister functions will affect ONLY the shell that the exec() call invokes, and anything you do in the shell.
<?php
$olddir = getcwd();
chdir('/path/to/new/dir'); //change to new dir
exec('somecommand');
will execute somecommand in /path/to/new/dir. If you do
<?php
exec('cd /path/to/new/dir');
exec('somecommand');
somecommand will be executed in whatever directory you started the PHP script from - the cd you exec'd just one line ago will have ceased to exist and is essentially a null operation.
Note that if you did something like:
<?php
exec('cd /path/to/new/dir ; somecommand');
then your command WOULD be executed in that directory... but again, once that shell exits, the directory change ceases to exist.
I have this c++ code
if(i>1)
{
std::system("php -f C:\\Program Files\\EasyPHP 12.1\\www\\CarProtectionSystem\\UpdateDetection.php");
}
I am using EasyPHP ... but when I run the code I have this error: php is not recognized as an internal or external command operable program or batch file.
Any Help ?
It's telling you that php.exe (or php.cmd or php.bat) isn't in the system path. Either add it to the path, or include the full path to the exe
(e.g. std::system("c:\\Program Files\\<whatever>\\php.exe -f ...)