Hi,

I'm trying to execute some commands via the exec / shell_exec / system / passthru / popen PHP functions.
The commands are getting executed successfully, but they're not returning any output, which they would normally do, if they are executed in a terminal/shell environment:

http://i.minus.com/ipySNqCnFn5iG.png
http://i.minus.com/ibmq8Cer2yOAOR.png

What I expect:

To execute a particular command and to get real-time raw output from the executed commands and display them to the browser/php page SIMULTANEOUSLY while the command is being executed.

So for example, if a file is being transferred via a command, and the program being executed is supposed to return:
3764520 (1%) 1.52M/s eta:2m [Sending data]

I need this exact information to be passed on to the browser / PHP script without any intervention. I am aware about the passthru function which should be used for this purpose, but all of the functions I've used return NO output AT ALL, while the commands itself get executed successfully without any problem.

What I get:

The browser window is totally empty and no output is displayed, even though the command itself was successfully executed.

What I have tried:

<?php

$command = "lftp -u username,password ftp.website.com -e 'set ftp:ssl-allow off; put /var/www/folder/file.zip; bye'" . " 2>&1";
$handle = popen ($command, 'r');

while (($buffer = fgets ($handle, 4096)) !== false)
{
echo $buffer . "\n";
ob_flush();
flush();
}

pclose ($handle);

?>

and

<?php

$command = "/usr/bin/lftp -u username,password ftp.website.com -e 'set ftp:ssl-allow off; put /var/www/folder/file.zip; bye'" . " 2>&1";
system ($command);

?>

and

<?php

$command = "/usr/bin/lftp -u username,password ftp.website.com -e 'set ftp:ssl-allow off; put /var/www/folder/file.zip; bye'" . " 2>&1";
exec ($command);

?>

Command(s) I'm trying to execute:

executes properly, but no output displayed:
lftp -u username,password ftp.website.com -e 'set ftp:ssl-allow off; put /var/www/folder/file.zip; bye' 2>&1

executes properly, and output is displayed:
ls -lsra /home/username/files/

Have you tried to add a second argument that will hold the output of the command:

$command = "/usr/bin/lftp -u username,password ftp.website.com -e 'set ftp:ssl-allow off; put /var/www/folder/file.zip; bye'" . " 2>&1";
exec($command, $output);
print_r($output);

Thanks for the assistance, however, I have tried that before.

It makes no difference, there is still no output to speak of. The command itself still gets executed properly, as was the case before ... just that there's no output being displayed.

I tried to mess around with other commands; 'wget' to be specific and this time to my surprise, output WAS being displayed correctly with this:

<?php

$data = shell_exec("wget http://speedtest.tweak.nl/10mb.bin 2>&1");
echo "<pre>";
echo $data;
echo "</pre>";

?>

However, using the same example as above with 'lftp' doesn't give any output.

Well, then maybe it's got to do something with the command itself and the redirections you use. Have you tried it without the 2>&1 part or executing each part of the command sepparately (now I am guessing :-)?

After extensive research, I have finally solved this.

Here's how:
- Some specific commands in Linux don't respond well with the different PHP functions meant to execute commands on the shell. What this means is, that the command itself would be executed, however, there would be no output caught by these PHP functions.
- The solution: make use of the 'script' command in Linux.
- The command that you would eventually formulate, should look something like this:
script -q -f -c "lftp -u username,password ftp.website.com -e 'set ftp:ssl-allow off; put /path/to/file.zip; bye'" > /path/to/file.log 2>&1 &
- store the above mentioned command in a variable and use it within a command execution PHP function, such as shell_exec();
- then, PHP will execute the command and as you can notice, we ran the command as a background process. This was deliberate, as we eventually wanted to find out the PID of the LFTP process, which would be impossible if you would try to directly find the PID by echo $!; since this method would always return an incorrect PID. This is because the actual LFTP process is started AFTER php loses control over it.
- So, how do we find the LFTP process again, via PHP in order to monitor it?
- The solution: via shell_exec (); run something like this:
ps aux | grep "lftp -u username,password ftp.website.com -e set ftp:ssl-allow off; put /path/to/file.zip; bye"
- this will find the LFTP process, but how do we extract the PID out of ALL the information we just received?
- The solution: use PHP's explode function to build an array using a single whitespace (" ") as a delimiter:
$ps_info_array = explode (" ", $ps_info);
- where $ps_info would contain the output of the ps aux | grep we did in the previous step.
- finally, ps_info_array[1] would contain the PID of the LFTP process which is currently running.
- now, we run a while loop, which would run until the LFTP process is running. We ensure this by using something like:

`while (file_exists("/proc/{$ps_info_array[1]}")){
}`
  • since /proc/PID is a directory always created, until a process is running on linux.
  • next, within the while loop, we need to use tail -c 79 to get the last line updated within the log file by LFTP. This will ensure we always get the latest progress as reported by LFTP.

$last_line = "tail -c 79 " . $log_file;
$currentline = shell_exec ($last_line);

  • we cannot use tail -n 1 to simply get the last line, as it would cumulate all the lines from the starting of the log file to the last line of the file and return everything, instead of just the last line. Probably due to the log file being saved being saved in the "MAC" format.

  • next, we use regular expressions to match for "(d%)" or "(dd%)" where d is a single or double digit number. This extracts the percentage completed value from the LFTP output and makes it available for use.

preg_match ("/\(\d%\)|\(\d\d%\)/", $currentline, $matches);

  • the most recent percentage will now always be available in $matches[0] while the loop is running.

  • note, that, we also need to use flush(); after we echo the percentage value just found, otherwise, PHP will wait for the while loop to finish (and therefore, for the LFTP transfer to complete before reporting anything) which will defeat the purpose of our script.

  • finally, after the file file is transferred successfully, LFTP generates a confirmation message such as:

  • 197735896 bytes transferred in 86 seconds (2.20M/s)
  • this regular expression match finds this line and reports to the client that the transfer is complete.

preg_match ("/bytes transferred in/", $currentline, $matches);
echo "<br />" . "File Transferred Successfully.";

commented: Great +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.