4

I know how to read files in PHP but I want to read the latest file that has been created in a specific folder.

I have a html button pointing to read.php

read.php reads the folder c:\file\ and reads the latest file that is created in that folder.

I couldn't find much information on how to do this

Can anyone help me doing that.

Ank
  • 6,040
  • 22
  • 67
  • 100

4 Answers4

16

Get the latest file in C:\file:

$files = glob('c:/file/*.*');
$files = array_combine($files, array_map('filectime', $files));
arsort($files);
echo key($files); // the filename 
flowfree
  • 16,356
  • 12
  • 52
  • 76
  • Oh the wonders of PHP. My outlier detection was written with Java influence, and Java lacks these shortcuts. Props; your answer is far cleaner than mine. Upvoted. – FThompson Jun 12 '12 at 01:42
  • To clear up any confusion, [`c` in `filectime()` does NOT stand for `creation`](https://www.php.net/manual/en/function.filectime.php). – mickmackusa May 31 '23 at 07:10
0

Iterate through the directory, keeping track of the most recently created file, returning the latest once every one has been iterated through and checked.

$dir = "C:\file\";
if ($handle = opendir($dir)) {
    $latest = null;
    while (($cur = readdir($handle) !== false) {
        if ($latest == null || filectime($cur) > filectime($latest)) {
            $latest = $cur;
        }
    }
    closedir($handle);
}
return $latest;

Note: Most Unix operating systems do not keep track of file creation date, so the closest you can get is last-modified date (via filemtime or filectime). However, on Windows, filectime returns the creation date, and filemtime returns the last-modified date.

FThompson
  • 28,352
  • 13
  • 60
  • 93
  • That returns the last modified file. You want to use `filectime` – sachleen Jun 12 '12 at 01:12
  • Oh, you're right, my bad. Also adding the note that many Unix operating systems don't track creation time, so filectime has the same result as filemtime in Unix. – FThompson Jun 12 '12 at 01:38
  • To clear up any confusion, [`c` in `filectime()` does NOT stand for `creation`](https://www.php.net/manual/en/function.filectime.php). – mickmackusa May 31 '23 at 07:10
0

This will iterate over each file in a directory and determine which file has the newest 'created time':

function find_latest()
{
    $l = 0;
    $r = '';
    foreach( new DirectoryIterator('C:\\file') as $file )
    {
        $ctime = $file->getCTime();    // Time file was created
        $fname = $file->getFileName(); // File name
        if( $ctime > $l )
        {
            $l = $ctime;
            $r = $fname;
        }
    }
    return $r;
}
Daniel Placek
  • 765
  • 5
  • 16
  • To clear up any confusion, [`C` in `getCTime()` does NOT stand for `creation`](https://www.php.net/manual/en/splfileinfo.getctime.php). – mickmackusa May 31 '23 at 07:11
0

If the directory has thousands of files, it is much faster to get the file at the OS level. The following code is OS-specific though and would work on a Unix-family OS only:

    chdir($dir); // change working dir
    $latest = exec('ls -t | head -1'); // get first filename from file list sorted by modification time
    if ($latest) {
        $latest = $dir . DIRECTORY_SEPARATOR . $latest;
    }

kiatng
  • 2,847
  • 32
  • 39
  • For Windows, you can test the command `ls -t | head -1` by pasting it in the cmd.exe terminal. It works for Wins 10 Pro. – kiatng Jun 01 '23 at 02:04