I'm attempting to download a file with wget using PHP exec to a specific directory and it is not working. No errors are showing either?
Here is my code:
$filename = 'test2.mp4';
$source = 'http://example.com/test.mp4';
var_dump(exec('wget -b -o '.$filename.' '.$source.' -p /home/mywebsite/public_html/assets/videos/ -h 2>&1', $output, $errno), $output, $errno);
What did i do wrong in my wget command?
-b so download runs in background
-o to rename the file to test2.mp4
-p to download to specific directory
How do i solve?
My Answer:
There are a few issues with your code.
1. The `-p` flag in wget is used to specify the directory prefix where all files and subdirectories will be saved to, not the destination directory for the downloaded file.
2. The `-h` flag is not a valid flag for wget.
3. You are not properly escaping the file name and source URL in the exec command.
Here is the corrected code:
php
$filename = 'test2.mp4';
$source = 'http://example.com/test.mp4';
$destination = '/home/mywebsite/public_html/assets/videos/';
$escapedFilename = escapeshellarg($filename);
$escapedSource = escapeshellarg($source);
$escapedDestination = escapeshellarg($destination);
exec("wget -b -o $escapedFilename $escapedSource -P $escapedDestination");
In this code snippet, we use `escapeshellarg` to properly escape the file name, source URL, and destination directory. We also use the `-P` flag to specify the destination directory for the downloaded file.
Rate this post
4 of 5 based on 8373 votesComments