Wget return loaded file name

I use wget in php script and should get the name of the downloaded file.

For example, if I try

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

I get the index.html file in the download directory.

EDIT: The page will not always be Google, although the target may be an image or a style sheet, so I need to know the name of the downloaded file.

I would 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"
?>
+3
source share
4 answers

I ended up using php to find the latest updated file in a directory using the following code:

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

$newstamp = 0;
$newname = "";
$dc = opendir($dir);
while ($fn = readdir($dc)) {
  # Eliminate current directory, parent directory
  if (ereg('^\.{1,2}$',$fn)) continue;
  $timedat = filemtime("$dir/$fn");
  if ($timedat > $newstamp) {
    $newstamp = $timedat;
    $newname = $fn;
  }
}
// $newname contains the name of the most recently updated file
// $newstamp contains the time of the update to $newname
?>
0
source

Maybe this is some kind of fraud, but why not:

  • choose the name of the file you want wgetto create
  • wget,
  • , - .

-O wget; -)


, :

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

my-output-file.html.

+3

if your requirement is as simple as getting it google.com, then do it in PHP

$data=file_get_contents('http://www.google.com/');
file_put_contents($data,"./downloads/output.html");
+1
source

On Linux 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 ./downloadsthere is no other file creation process in the directory .

0
source

Source: https://habr.com/ru/post/1738132/


All Articles