How can I run a linux command from a PHP script
Running external system commands from within a PHP application is a common requirement when interacting with the filesystem, managing packages, or automating deployment tasks on Linux servers. The goal of your request is to execute wget and potentially capture its output directly within the script logic.
To achieve this, you must use a built-in PHP function that communicates with the operating system's shell. While there are several options available in the standard library, they behave differently regarding how they handle the command stream and return values. The most appropriate functions for this task are shell_exec(), exec(), and system().
Using shell_exec() to Capture Output
If your primary objective is to echo the output of the command back to the script or store it in a variable, shell_exec() is the cleanest choice. This function executes the given command and returns its complete output as a string. If the command fails or produces no output, it returns an empty string rather than false (unlike exec in certain configurations), which makes parsing easier.
Here is how you would implement your specific use case:
$output = shell_exec('wget http://www.example.com/image.jpg /folder');
if ($output !== '') {
echo $output;
} else {
echo "done";
}
In this example, the wget command is passed as a single string. PHP spawns a new process to run it. The resulting output (stdout) from wget is captured and stored in the $output variable. If you simply want to print the success message regardless of output, checking for an empty string or using a simple conditional works effectively.
Handling Command Output Directly
You asked if you could echo the output directly. Yes, but be aware that shell_exec() does not automatically append a newline character to the end of its return value in all PHP versions. If your downstream process expects standard text formatting, you might need to manually add one:
echo shell_exec('wget http://www.example.com/image.jpg /folder') . "\n";
Important Trade-offs and Pitfalls
When selecting a function, you must consider how the command's exit status and error messages are handled.
exec() vs. shell_exec()
The exec() family of functions can be more complex because they allow you to capture separate streams for standard output (stdout) and standard error (stderr). This is useful if your command produces warnings that you want to log separately from the successful output. However, shell_exec() is generally preferred when you only care about the combined result.
A critical security consideration involves how arguments are passed. While passing a single string like 'wget http://example.com/file' works, it can be vulnerable to shell injection if any part of that string comes from user input. For instance, if $url contains http://example.com?; rm -rf /, blindly echoing it into the command string could execute arbitrary code. Always use escapeshellarg() when constructing commands with dynamic data:
$url = escapeshellarg($_GET['url']);
$folder = escapeshellarg('/folder');
$output = shell_exec("wget {$url} {$folder}");
Configuration and Permissions
Remember that PHP scripts run under specific user permissions (often www-data or apache), which may differ from your logged-in user. If the command fails, it is frequently due to permission issues preventing the web server user from writing to /folder. Ensure the target directory exists and has write permissions for the PHP process user.
Additionally, check that the system actually has wget installed in the environment where the script runs. In Debian environments, this usually means verifying with apt list --installed wget before relying on it in production code.
For complex orchestration needs, or if you are building a robust application architecture, consider frameworks like Laravel which provide better abstractions for running shell commands via the Process facade, offering more control over streams and timeouts without needing to manually handle exit codes as strictly as raw PHP functions.