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");
Related
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.
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);
I have come across an interesting situation with the shell_exec() function in PHP. This command works:
$res = shell_exec("mdb-export -D \"%d/%m/%y\" ".$this->mdbFileName." ".$tableName." &> " . $outputFile);
However, this command does not work:
$res = shell_exec("mdb-export -D \"%d/%m/%y\" ".$this->mdbFileName." \"".$tableName."\" &> " . $outputFile);
When entering the command in a shell (either way - with or without quotes around the table name) it works fine. I would really like to know why the first set of escaped quotes does not affect shell_exec():
\"%d/%m/%y\"
But the second set:
\"".$tableName."\"
Does not allow shell_exec to execute. FYI: all file name variables in the script are full file paths. They were entered in the exact same way when testing in the shell. Any thoughts?
EDIT: Upon failure, shell_exec returns NULL and the command is not executed.
After some more testing, it appears that the $tableName variable contained an extra space. Another part of the program was pulling the table name from the MDB file and left a trailing space.
When echoing out the $tableName variable everything appeared fine because the space was invisible. When running shell_exec without quotes surrounding the table name, the command executed because no errors occur if there are extra spaces between parameters. However, when surrounding the variable with quotes, the mdb-export program started looking for:
"tableName "
instead of
"tableName"
causing a silent, yet complete failure.
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");
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';