So I have this PHP code which is pretty simple.
$string = exec("ls foo");
In foo I have 4 files
foo
bar
hi
bye
But echo $string returns bye
How can I make it return all of the files? Is it not working because ls separates by tabs?
From the manual: http://php.net/manual/en/function.exec.php
Return Values
The last line from the result of the command. 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.
Please do not use exec for file operations. PHP has a full set of functions for that purpose. You can start with dir:
http://php.net/manual/en/function.dir.php
Boris' suggestion is good, but it's a bit complicated to figure it out.
Simply use this
passthru ('ls');
or this to 'prettify' it
echo "<pre>";
passthru ('ls');
echo "</pre>";
Related
I have a PHP file that runs a node script using exec() to gather the output, like so:
$test = exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/");
echo $test;
It outputs a JSON string of data tied to the website in the --url paramater. It works great, but sometimes the output string is cut short.
When I run the command in the exec() script directly, I get the full output, as expected.
Why would this be? I've also tried running shell_exec() instead, but the same things happens with the output being cut short.
Is there a setting in php.ini or somewhere else to increase the size of output strings?
It appears the only way to get this working is by passing exec() to a temp file, like this:
exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/ > /home/user/www/uploads/json.txt");
$json = file_get_contents('/home/user/www/uploads/json.txt');
echo $json;
I would prefer to have the direct output and tried increasing output_buffering in php.ini with no change (output still gets cut off).
Definitely open to other ideas to avoid the temp file, but could also live with this and just unlink() the file on each run.
exec() only returns the last line of the output of the command you pass to it. Per the section marked Return Value of the following documentation:
The last line from the result of the command. 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.
To get the output of the executed command, be sure to set and use the output parameter.
https://www.php.net/manual/en/function.exec.php
To do what you are trying to do, you need to pass the function an array to store the output, like so:
exec("/usr/local/bin/node /home/user/www/bin/start.js --url=https://www.example.com/", $output);
echo implode("\n", $output);
I have a program running on my command line. The program allows one to type in a username and returns a bunch of information pertaining to that user. I assume it is possible to access this program from php, and even store the information in a variable. Anyone know how to do this though?
Thanks in advance for the advice
This link should help you. A quick example:
<?php
$out = array();
exec = ('ls /ver 2>&1', $out);
print_r($out);
?>
try using the system() command.
http://php.net/manual/en/function.system.php
or exec()
http://www.php.net/manual/en/function.exec.php
Read the documentation for system() and exec(), it should what you need.
I'm using system() PHP function to run some curl commands like this system("curl command here",$output); but it displays results on screen. Any way to avoid this output?
You're using the wrong function for that. According to the docs:
system() is just like the C version of the function in that it executes the given command and outputs the result.
So it always outputs. Use execDocs instead which does return (and not output) the programs output:
$last = exec("curl command here", $output, $status);
$output = implode("\n", $output);
Or (just for completeness) use output bufferingDocs:
ob_start();
system("curl command here", $status);
$output = ob_get_clean();
You coud try using output buffering.
ob_start();
system("curl command here",$output);
$result = ob_get_contents();
ob_end_clean();
You could either modify the command string and append " 1>/dev/null 2>&1" or - more elegantly - execute the process with a pipe (see example #2).
For a more refined control over the process' file handles, you can also use proc_open().
The system function displays the output from your command, so you're out of luck there.
What you want is to change system for exec. That function will not display the command's output.
No, you should use PHP curl library
I want to see the output of the following code in the web browser:
code:
<?php
$var = system('fdisk -l');
echo "$var";
?>
When I open this from a web browser there is no output in the web browser. So how can I do this? Please help!
Thanks
Puspa
you can use passthru, like so:
$somevar = passthru('echo "Testing1"');
// $somevar now == "Testing1"
or
echo passthru('echo "Testing2"');
// outputs "Testing2"
use exec('command', $output);
print_r($output);
First of all, be sure that you (=user under which php runs) are allowed to call the external program (OS access rights, safe_mode setting in php.ini). Then you have quite a few options in PHP to call programs via command line. The most common I use are:
system
This function returns false if the command failed or the last line of the returned output.
$lastLine = system('...');
shell_exec or backtick operators
This function/operator return the whole output of the command as a string.
$output = shell_exec('...'); // or:
$output = `...`;
exec
This function returns the last line of the output of the command. But you can give it a second argument that then contains all lines from the command output.
$lastLine = exec('...'); // or capturing all lines from output:
$lastLine = exec('...', $allLines);
Here is the overview of all functions for these usecases: http://de.php.net/manual/en/ref.exec.php
I am rather new to using the command line and php. That being said I have been trying to figure out how to use ImageMagick with the exec() function. I have this currently,
$command="/usr/local/lib/ImageMagick convert images/a.pdf images/a.png";
if(exec($command)){
echo 'yes';
}
else{
echo 'no';
}
Which is returning 'no'. I believe I am missing something about how to execute convert from the correct directory. Is my $command set up properly? (I was given the path to ImageMagick from my web host, Lunarpages).
I have read through some of the other questions regarding ImageMagick but I haven't found much to help me set up my command.
Thanks for any help,
Levi
What your command is currently attempting to do is execute a program named /usr/local/lib/ImageMagick which I am guessing is not what you were intending. If that is the path to ImageMagick and you want to use the convert utility you need to modify your command to the following:
/usr/local/lib/ImageMagick/convert images/a.pdf images/a.png
At which point it should work without any issues! You may want to dig further into what the convert command can do for you!
use the exec() the correct way as your exec returns a string by default and the execution results is passed back via one of it's parameters as such :
$command="/usr/local/lib/ImageMagick/convert images/a.pdf images/a.png";
exec($command,$output,$result);
if ($result == true ){
echo 'yes';
}
else{
echo 'no, here's what happened with command output';
print_r($output);
}
refer to http://php.net/manual/en/function.exec.php