How to run wget from php so that output gets displayed in the browser window?

How to run wget from php so that output gets displayed in the browser window?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Redirecting Wget Output to the Browser in PHP

Running wget from a PHP script often feels like trying to force a Unix command-line tool into a web context. The default behavior of exec(), shell_exec(), or system() is to capture output into a variable, discard it, or return only the exit code. If your goal is to display that raw HTML or text stream directly in the browser window while the script runs, you are essentially asking for real-time streaming, which conflicts with how wget typically operates as a synchronous blocking process.

The most direct approach involves passing the output of the command directly to the PHP output buffer. When using functions like system(), the output is printed immediately as it is generated by the underlying process. However, wget downloads files sequentially; if you want to see the progress or the content stream without waiting for the download to finish before rendering anything, simple redirection isn't enough. You need to ensure the PHP process does not buffer its own output and that the shell doesn't intercept the stream.

Consider this basic implementation:

<?php
$command = 'wget -q --show-progress https://example.com/file.txt';
$output = system($command);

echo $output;
?>

In this example, system() executes the command and prints whatever wget writes to stdout. The -q flag is often omitted when you want to see progress bars or status messages, as quiet mode suppresses them. By removing -q and adding --show-progress, you get visual feedback on the CLI side. However, there is a critical nuance: if wget downloads a large file, the browser might freeze waiting for the entire stream before displaying anything, depending on how the client handles long-running requests.

A more robust solution involves piping the output through PHP's standard streams while managing timeouts and headers properly. Since wget returns an HTTP status code and the downloaded content in separate streams or variables depending on flags, you might want to capture the status first to check for success before attempting to display the body. For instance:

<?php
$command = 'wget -O - https://example.com/file.txt 2>&1';
$handle = popen($command, 'r');

if ($handle) {
    while (!feof($handle)) {
        $buffer = fread($handle, 8192);
        echo $buffer;
        ob_flush();
        flush(); // Force immediate output to the browser
    }
    fclose($handle);
} else {
    echo "Failed to execute command.";
}
?>

Here, popen() creates a pipe between the PHP script and the shell process. Reading in chunks with fread() prevents memory exhaustion on large downloads. The crucial part is flush(), which pushes buffered output directly to the client. Without this, PHP's internal buffering might delay content until the script finishes or hits a specific size limit.

One significant pitfall is that many modern web servers and frameworks buffer output by default to improve performance. If you are running inside a Laravel application, for example, the response might be buffered before reaching the browser. In such environments, you may need to disable buffering explicitly using ob_implicit_flush(true) or configuring the server to not buffer responses for CLI-invoked scripts.

Another consideration is error handling. wget writes errors and progress information to stderr, while content goes to stdout. Using 2>&1 redirects stderr to stdout in the shell, allowing both streams to mix. If you want to handle errors separately, capture them before echoing. This distinction becomes vital when debugging why a download failed silently or displayed partial content.

If your stack includes Laravel, integrating CLI tools into web requests requires careful attention to how the framework handles output buffering and response headers. While https://laravelcompany.com provides resources on building robust PHP applications, keep in mind that mixing CLI utilities with web responses demands explicit control over streams.

Our network