views:

401

answers:

3

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"
?>
+2  A: 

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.

Pascal MARTIN
+1 - Solving problems often entails asking yourself if you are solving the right problem :)
Tim Post
Good solution, but I should have clarified that the target of wget may be an image or stylesheet, or any other file. I updated the question to reflect this.
Matthew
A: 

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");
ghostdog74
A: 

On Linux like systems you can do:

system('/usr/bin/wget -q --directory-prefix="./downloads/" http://www.google.com/');
$filename = system('ls -tr ./downloads'); // $filename is now index.html

This works if there is no other process creating file in the ./downloads directory.

codaddict
do you really need to call system `ls` to do directory listing in PHP ? :) How about PHP's own `readdir()` or `glob()`
ghostdog74