recent-file-list

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;
  }
}

Solution

  • You need to replace this code:

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

    With this:

    $path = "myfolder";