Using Php to execute command line - php

I got some problem while using php to execute command line.
I have a software and I need to execute the software to export pdf by using cmd
command line.
(The software need to be executed by using command-line)
Therefore i wrote a php code.
<?php
$cmd ='C:\XmlServer.exe C:\input.xml';
shell_exec($cmd);
?>
I've tried the string 'C:\XmlServer.exe C:\input.xml' is working on cmd.
But I can't using php to execute command line to get the same result.
I also tried exec($cmd);, but it still didn't work.
Could anyone help me solving the problem?
I want to run php just like running command line.
I wrote like the following in command line:
C:\> XmlServer.exe input.xml
then it's ok to output a file for me.
but using the same code in php didn't work.
--
Update
using echo shell_exec($cmd); is okay, but no output.

try with echo like that :
<?php
$cmd ='C:\XmlServer.exe input.xml';
echo exec($cmd);
?>

Related

Ubuntu 20 terminal PHP script output

Ubuntu 20 running on WSL, PHP 7.4
I am trying to debug code but cannot seem to get any output from the script.
To replicate:
myfile.php
<?php
echo "output text";
called from the terminal with
php myfile.php
The script runs but returns nothing.
You are trying to do int in right way.
You can tell php to execute a file like the following:
php myfile.php
... or
php -f myfile.php
You can check the following two things.
First one is that make sure you have installed php cli.
In your script try to put that line #!/usr/bin/php on very top of the file. So your file will looks like the following:
#!/usr/bin/php
<?php
echo "output text";
Hope that helps you or at least gives you a right direction.
For Debug your system needs some tools.
Run: php -v and see your php version.
You need to make sure you have module php installed.
Install: phpx.x-cli

PHP shell_exec is not responding on python command line

Hi I am running shell_exec on my PHP Application,
Here is the code,
$path = "C:/scripts/";
chdir($path);
$py_commonscript = 'python Common_Script.py';
$exec = shell_exec($py_commonscript);
echo "<pre>$exec</pre>";
This code doesn't give any result and does not generate a file.
When I tried running manually on the command line it is working and was able to generate a file.
I tried to execute this one below and it was able to display some result.
$sample = shell_exec('ls -lart');
echo "<pre>$sample </pre>";
I am wondering why the command for $exec is not being triggered or run.
The most likely cause is that your PHP code isn't inheriting a %PATH containing python.exe. Try print getenv('PATH'); or just use the full path to python.exe in your command line.

PDFtk command not running from exec

I've moved my code form one WAMP computer to another and the code that runs pdftk stopped working. I've compared the permissions on pdftk.exe and they are the same on both machines. When I run the same command from a command prompt, it works. I add exec("whoami") to the script and the user is the same on both computers. When I run something like exec('dir 2>&1', $out) it executes so I know exec is working form within php.
I've created a trivial php file to test and it doesn't work.
<?php
$String = 'pdftk.exe > "c:\temp\temp.txt"';
exec("$String");
exec("pdftk.exe > \"c:\temp\temp.txt\"");
?>
Both exec command result in a 0 byte file being created.
if I run
pdftk.exe > "c:\temp\temp.txt"
from a command line it puts the output of pdftk.exe into the temp.txt file as expected.
This seems like is must be some kind of permissions issue, but permissions on the executable seem to be the same. Losing my mind on this.
In my opinion, first line should be:
<?php
$String = 'pdftk.exe > "c:\temp\temp.txt"';
exec($String); //removed quotes
?>
And for the second line, what if you try this?
<?php
exec('pdftk.exe > "c:\temp\temp.txt\"', $outputAndErrors, $returnValue);
var_dump($outputAndErrors);
?>
Or if you remove your first ">"?
<?php
exec('pdftk.exe "c:\temp\temp.txt\" 2>&1', $outputAndErrors, $returnValue);
var_dump($outputAndErrors);
?>
These are some tests that may help you, thought.

exec in php is working inappropriately

I am willing to execute a shill command through PHP but i faced that the command is not executing , here is the command:
exec('/cutycapt/CutyCapt --url="' . $source . '" --out="/home/user/NetBeansProjects/PhpProject1/htmlImage/example.png"');
i tried as testing to execute the following :
echo exec(' ls /cutycapt/');//print_r is the same
only one file returned while this command returned them all
echo system(' ls /cutycapt/');
i tried to use the "system" method instead of exec in the first command and the result was the same
what could affect the command so it wan't execute ?
update
the case i'm talking bout the the first command work whether i run it in the terminal or i run the PHP script in terminal too but when i run it from the browser (the php script )it doesn't work !!
Look into manual - http://de3.php.net/manual/en/function.exec.php
exec and system returns "The last line from the result of the command"
In case of system and exec the last line from the result of the command gets returned.
If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
I had the same problem working with external commands in php. The problem was related to file permissions. I was using "vchiq" library and the error was "* failed to open vchiq instance". This page might work for you.

help with exec command via PHP

I'm trying to run a PHP exec command. It runs like a charm in my CMD but it doesn't do anything when I try it via PHP. Can someone see what I'm doing wrong here.
Thanks.
<?php
//Command line command
//"C:\Program Files (x86)\PHP\v5.3\php-cgi.exe" "C:\inetpub\wwwroot\dev_site\images\0000\thumbs.php"
//This runs perfectly fine.
echo "Command line execution started<br />";
//This is when $desination is already set to 0000
echo exec("C:\\Program Files (x86)\\PHP\\v5.3\\php-cgi.exe C:\\inetpub\\wwwroot\\dev_site\\images\\$destination\\thumbs.php");
echo "<br />Command line command successful";
//Does not run
?>
What's in your exec call is not the same as what's in your comment as the command. You got rid of the sets of quotes around the command and its argument. They may have been important.
In Windows, exec() issues an internal call to "cmd /c your_command". This implies that your command must follow the rules imposed by cmd.exe which includes an extra set of quotes around the full command. Hope these links will be helpful
http://php.net/manual/en/function.exec.php
http://ss64.com/nt/cmd.html
when you execute two or mor commands you must seperate them
try this:
echo exec("C:\\Program Files (x86)\\PHP\\v5.3\\php-cgi.exe", "C:\\inetpub\\wwwroot\\dev_site\\images\\$destination\\thumbs.php");

Categories