Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wget return downloaded filename

I'm using wget in a php script and need to get the name of the file downloaded.

For example, if I try

<?php
  system('/usr/bin/wget -q --directory-prefix="./downloads/" http://www.google.com/');
?>

I will get a file called index.html in the downloads directory.

EDIT: The page will not always be google though, the target may be an image or stylesheet, so I need to find out the name of the file that was downloaded.

I'd like to have something like this:

<?php
  //Does not work:
  $filename = system('/usr/bin/wget -q --directory-prefix="./downloads/" http://www.google.com/');
  //$filename should contain "index.html"
?>
like image 727
Matthew Avatar asked Feb 27 '26 16:02

Matthew


2 Answers

Maybe that's some kind of cheating, but why not :

  • decide yourself the name of the file that wget should create
  • indicate to wget that the download should be made to that file
  • when the download is finished, use that file -- as you already know the name.

Check out the -O option of wget ;-)


For example, running this from the command-line :

wget 'http://www.google.com/' -O my-output-file.html

Will create a file called my-output-file.html.

like image 195
Pascal MARTIN Avatar answered Mar 01 '26 06:03

Pascal MARTIN


if your requirement is simple like just getting google.com, then do it within PHP

$data=file_get_contents('http://www.google.com/');
file_put_contents($data,"./downloads/output.html");
like image 39
ghostdog74 Avatar answered Mar 01 '26 06:03

ghostdog74



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!