简体   繁体   中英

How to get newest file in remote folder PHP?

I try to get the most recent file of a folder but it does not work. My folder has an URL with html://... Can it be the problem ? This is what i tested ... thanks

$files = scandir('http://wwww.site.com/myfolder', SCANDIR_SORT_DESCENDING);
$newest_file = $files[0];

OR

$path = "http://wwww.site.com/myfolder"; 

$latest_ctime = 0;
$latest_filename = '';    

$d = dir($path);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path}/{$entry}";
  // could do also other checks than just checking whether the entry is a file
  if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
    $latest_ctime = filectime($filepath);
    $latest_filename = $entry;
  }
}

You need to replace this code:

$path = "http://wwww.site.com/myfolder";

With this:

$path = "myfolder"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM