• 13
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Get the latest file addition in a directory

How to get the latest file name, or the file path that is added into a directory?

php
$path = "/path/to/my/dir"; 

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

// now $latest_filename contains the filename of the file that changed last
  • 56
Reply Report
$dir = dirname(__FILE__).DIRECTORY_SEPARATOR;
$lastMod = 0;
$lastModFile = '';
foreach (scandir($dir) as $entry) {
    if (is_file($dir.$entry) && filectime($dir.$entry) > $lastMod) {
        $lastMod = filectime($dir.$entry);
        $lastModFile = $entry;
    }
}
  • 6
Reply Report
$dir = "/path/to/Your/dir";         
$pattern = '\.(zip|ZIP|pdf|PDF)$'; // check only file with these ext.          
$newstamp = 0;            
$newname = "";

if ($handle = opendir($dir)) {               
       while (false !== ($fname = readdir($handle)))  {            
         // Eliminate current directory, parent directory            
         if (ereg('^\.{1,2}$',$fname)) continue;            
         // Eliminate other pages not in pattern            
         if (! ereg($pattern,$fname)) continue;            
         $timedat = filemtime("$dir/$fname");            
         if ($timedat > $newstamp) {
            $newstamp = $timedat;
            $newname = $fname;
          }
         }
        }
closedir ($handle);

// $newstamp is the time for the latest file
// $newname is the name of the latest file
// print last mod.file - format date as you like            
print $newname . " - " . date( "Y/m/d", $newstamp); 
  • 3
Reply Report

Here's how you can do it using DirectoryIterator:

foreach(new DirectoryIterator('/path/to/read') as $item) {
    if ($item->isFile() && (empty($file) || $item->getMTime() > $file->getMTime())) {
        $file = clone $item;
    }
}

The resulting contents of $file is an instance of the DirectoryIterator class and as such you have access to all of it's methods. To simply get the full path of the result you can do:

echo $file->getPathname();
  • 1
Reply Report

Enumerate all directory files, get the filemtime() of each and you are done.

  • 0
Reply Report
    • @Gumbo: why use filectime instead of filemtime. I see filemtime() work better on a multitude of platforms while filectime() does not seem to work properly on all Windows servers.
      • 2
    • @secretlm filectime probably doesn't work because its only the creation time of the file, not the modification time

My solution with PHP 5:

$dir = "/path/to/Your/dir"; 
$arraydir =scandir($dir, 1);
echo "arraydir 0: " . $arraydir[0] . "<br/>"; // 1. new file
echo "arraydir 1: " . $arraydir[1] . "<br/>"; // 2. new file
echo "arraydir elements: " . count($arraydir) . "<br/>";

(search words DE: neuste und zweitneuste Datei eines Ordners anzeigen mit PHP )

  • 0
Reply Report
    • This sorts by name, will work only if you have alphabetically sortable timestamps in the filename, like exported_data_20170721.csv

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions