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");
Related
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.
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);
?>
I have tried to use exec() with 'whoami' to check if it works and I got the result of
nt authority\system
Now I need to run a .exe file with parameters from php via exec() function.
I tried this in command prompt and it actually runs the program with given parameters. This is the example command.
NOTE the exe file gets 3 inputs (folder, file_name, report_file_nmae)
> ..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml
But when I run this command from php file:
exec('..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml');
nothing is happening. This is the first time I am using exec() function, so I am not familiar with its details. What is wrong?
I tried using:
\\ instead of \
escapeshellarg() on the directory
added "" around directory folder names
No luck
Addendum:
echo exec($command) // echos < .... why?
or
exec($command, $output);
print_r($output); // Array()
I even changed the permission on the file to full control to all users.
If I call the program from command prompt, I can see the icon appearing next to clock for a second.
But the same call from php will not even call the program.
Edit
Even exec('notepad.exe'); is not working. Something has to be done with php configurations maybe?
I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().
Thanks #mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.
So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.
For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.
exec('some_command 2>&1', $output);
print_r($output); // to see the response to your command
Thanks for all the help guys, I appreciate it ;)
You might also try giving the full path to the binary you're trying to run. That solved my problem when trying to use ImageMagick.
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.
I have this PHP script (test.php):
<?php
$cmd = "/usr/bin/sass --watch file1.scss";
system($cmd);
?>
Now I call my PHP script from CLI this way:
/usr/bin/php test.php
And I get no output (it should print SASS is watching for changes).
If I call the SASS command directly from the shell, it outputs correctly.
Why?
Info: I'm using the PHP 5.3.6 version on OS X Lion
Edit: Please, note that this command watches for changes, it seems to behave differently to a regular command.
Edit2: The command works, it compiles correctly. The only thing lacking is the output (I want to debug and see errors :)
Some command line utilities like sass, manipulate the output pipe in some way that PHP can't use.
So, in this particular case, there is no solution.
system() returns a string. Just echo it.
According to http://se.php.net/system you need to pass in a second argument to system() and the return value of the command will be set in that variable:
<?php
system($command, $return);
echo $return;