PHP Code ('exec("echo $HOME")') cannot get HOME environment variable - php

Some PHP Code cannot get HOME environment variable.
getenv('HOME'); // OK
exec("env",$out);
print_r($out,1); // OK
exec("echo $HOME", $out);
print_r($out,1); // NG

It's because your string is in double quotes and PHP is substituting the value of the variable $HOME. Try changing the string to use single quotes. I.e.
exec('echo $HOME', $out);

Related

Get PHP output on Batch file

Found this nowhere else.
I have a batch file which calls a PHP file (php.exe - f file.php).
I'm blocked as i want to pass the PHP output (0 or 1) to the batch file.
Any idea on how to do this?
Thanks
In PHP write:
<?php
echo 'Done';
exit(0);
// Use a value >= 1 for errors
//exit(1);
Then use the %ERRORLEVEL% variable within the batch file to get the result (error level) from PHP.
#echo off
php.exe -f test.php
echo %ERRORLEVEL%
in the .bat file you can assign any output to the variable like this:
FOR /F "usebackq tokens=*" %%x IN (`php.exe -f test.php`) do (SET "VARIABLE=%%x")
echo result is %VARIABLE%
VARIABLE is an arbitrary variable name inside the batch
usebackq parameter allows to put whole command in the back quotes (`) and use double quotes (") inside as parameters.

ERROR_CACHE_MISS when passing value from PHP to bash

I am trying to pass some values from a PHP file to a BASH script. I am getting a ERROR CACHE_MISS response.
The variable 'coretown' holes the value 'Houston, TX'. It must be in that format for the bash script to work.
Results of a test to prove the variables are correct
WorkString531cdf6b8b3451.99781853 OutString531cdf6b8b3451.99781853 Houston, TX
Execute the bash script.
$errorTrap=shell_exec("./Find-Town.sh $workPath $outPath $coreTown");
Bash script:
#!/bin/bash
set -x
InFile="./zipcode.txt"
"$Work1"="$1"
"$OutFile"="$2"
"$InString"="$3"
echo "$1";
echo "$2";
echo "$3";
Returned by the 'echo' in the script:
WorkString531cdf6b8b3451.99781853 OutString531cdf6b8b3451.99781853 Houston,
Notice the state (TX) is missing. If I put 'echo "$4";' in there it will display the 'TX'.
Is one of these languages handling the content of 'coreTown' ('Houston, TX') as an array? If so, which one? Amd how do I fix it? My google searches did not address this problem.
Since $coreTown contains a space, that's being treated as an argument delimiter in the shell; you need to quote it or escape the space. Luckily, PHP has a function that does that for you: escapeshellarg.
$workPathEsc = escapeshellarg($workPath);
$outPathEsc = escapeshellarg($outPath);
$coreTownEsc = escapeshellarg($coreTown);
$errorTap = shell_exec("./Find-Town.sh $workPathEsc $outPathEsc $coreTownEsc");

How to run an ".exe" file throw PHP class

how can i run an ".exe" file throw my PHP class?
Or is there any other way to do that?
I have already used
exec();
passthru();
function but its give me error, my code :
$path = "C:\Program Files\Softland\doPDF 7\dopdf.exe";
$result = passthru('$path 2>&1');
Error :
'$path' is not recognized as an internal or external command, operable program or batch file.
This isn't a good practice but ....
$result = passthru($path.' 2>&1');
or
$result = passthru("$path 2>&1");
you should (as you can see) put $path variable outside single quotes (that prevent variable evaluation and substitution) or put double qoutes (that permits variable substitution)
You need to use double quote if you want $path to be replaced by variable value so
$result = passthru("$path 2>&1");
The problem is that you're using single quotes, so the string passed to the shell was literally '$path 2>&1'. Variable substitution does not occur in side single quotes, only double, in PHP.
So you need to either concatenate the string or put it in double quotes.
$result = passthru("$path 2>&1");

Passing a path in a PHP CLI script argument

I'm writing a php CLI script that accepts, among other argument, a path.
So an example is:
php myscript.php -p=/Volumes/Macintosh HD/Users/andrea/samples
The script has his own way to red the arguments and it properly gets the value for -p, setting it in a variable called $project_path.
However, when I test the folder with isdir($project_path) it returns false.
I've tried to pass the path in different ways:
/Volumes/Macintosh\ HD/Users/andrea/samples
'/Volumes/Macintosh HD/Users/andrea/samples'
"/Volumes/Macintosh HD/Users/andrea/samples"
'/Volumes/Macintosh\ HD/Users/andrea/samples'
"/Volumes/Macintosh\ HD/Users/andrea/samples"
Non of them works.
What's the format I must use to make it work?
Please consider that the script must also work on different OS (i.e. Windows).
The problem is the path argument is automatically escaped:
I need to unescape it.
The returned string is:
\'/Volumes/Macintosh\ HD/Users/andrea/samples\'
Short answer: Use escapeshellarg()
Long answer:
chmod +x yourscript.php
-
$path = '/Volumes/Macintosh HD/Users/andrea/samples';
$cmdline = sprintf('/home/user/yourscript.php -p=%s 2>&1', escapeshellarg($path));
$output = shell_exec($cmdline);
Example cli script:
#!/usr/bin/php
<?php
fwrite(STDOUT, print_r($_SERVER, TRUE));
exit(0); // exit with exit code 0
?>
I eventually used the getopt() to get unescaped arguments (I don't know why there is this difference) and str_replace( array( "'", '"'), '', $file_path ); to remove the wrapping quotes.

How do I to properly handle spaces in PHP Shell_exec?

I'm running on win2003 server, PHP 526, via the cmd-line.
I have a cmdline string:
$cmd = ' "d:\Prog Files\foo.exe" -p "d:\data path\datadir" ';
Trying to do this in php code
$out = `$cmd`; # note use of backticks AKA shell_exec
results in a failure by foo.exe as it interprets the -p arg as "d:\data".
However, the same $cdm string copied to the windows shell cmdline executes successfully.
How do I properly handle spaces in PHP shell_exec?
Use escapeshellarg() to escape your arguments, it should escape it with an appropriate combination of quotation marks and escaped spaces for your platform (I'm guessing you're on Windows).
Unlike Unix, which requires quotes within quotes to be escaped, Windows doesn't seem to work this way. How it keeps track of the quotes is beyond me, but this actually seems to work, despite all logic to the contrary:
$cmd = '" "C:\Path\To\Command" "Arg1" "Arg2" "';
$fp = popen($cmd, 'r');
$output='';
while ($l = fgets($fp, 1024))
$output.=$l;
I'm guessing command.exe is taking the command string total and nixing the (otherwise redundant) outside quotes. Without these outside quotes, DOS does some weird things. This solution is similar to post by user187383, but does away with the "cmd /c" which only obfuscates what's actually happening along with a mild performance cut, since cmd /c is implicit by the shell call in the first place!
This is an interesting problem. Apparently, PHP lets you put double quotes around the program or the arguments, but not both. It may be worth reporting this as a bug.
A work around is to use the DOS 8.3 name instead of quotes. E.g., "C:\Program Files\" usually becomes "C:\Progra~1".
Had this problem too - came up with an idea to route the launching through cmd.exe. The trick here is not to get lost in the double qoutes. Generally you want to put anything you want to run in:
exec('cmd /c " '.$path.' "';
Where $path is a already double-quoted path to your executable. Example:
$path = '"C:\Program Files\ToDoList Simple\ToDoList.exe" "C:\SomePath\todo.tdl" -nt test -cm test2';

Categories