Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - shell_exec output in browser is empty

I'm running a simple wget command in shell_exec()

wget_file.php

<?php
$command = "wget http://mydomain.co/media/bigbigbig.wav";
$output = shell_exec($command);
echo $output;
?>

According to http://www.php.net/shell_exec I can possibly expect an output: "The output from the executed command or NULL if an error occurred or the command produces no output."

If I run wget_file.php from the command line, I'll see a display of the wget results. However, if I run it from a browser, no result is given. (but the file does indeed download successfully)

I plan to execute the wget_file.php by calling via cUrl, while passing the url + path. But would be nice to get a response from the shell_exec(), after execution is completed.

Does anyone know if I'm just missing something to get an output (running in the browser)?

like image 423
coffeemonitor Avatar asked Mar 22 '23 07:03

coffeemonitor


1 Answers

If I run wget_file.php from the command line, I'll see a display of the wget results

wget doesn't output the file contents to console by default, so presumably you're talking about the status information.

I'd imagine this is output to STDERR rather than STDOUT, and it's STDOUT that shell_exec obtains for you in your PHP script:

  • when you run your script from the command line, you're still seeing the output because both streams are shared by the single terminal window; it's just that you're seeing it directly from wget and not from your PHP script;

  • in the case of passing it through Apache and to a browser to satisfy a web request, this terminal context is disconnected from the result the user sees.

In your shell command you can redirect the former to the latter:

$command = "wget http://mydomain.co/media/bigbigbig.wav 2>&1";

The comments on the documentation for shell_exec touch on this, particularly the — er — very first one!

like image 190
Lightness Races in Orbit Avatar answered Apr 01 '23 22:04

Lightness Races in Orbit