1

我有一个文件夹保存在特定目录中,它包含一些上传的图像,通过使用 php,我如何使用其路径访问该文件夹并返回该文件夹中最新添加(上传)的图像?

4

2 回答 2

2

您必须扫描整个目录并找到最新的文件:

function getLatestFile($directoryPath) {
    $directoryPath = rtrim($directoryPath, '/');

    $max = ['path' => null, 'timestamp' => 0];

    foreach (scandir($directoryPath, SCANDIR_SORT_NONE) as $file) {
        $path = $directoryPath . '/' . $file;

        if (!is_file($path)) {
            continue;
        }

        $timestamp = filemtime($path);
        if ($timestamp > $max['timestamp']) {
            $max['path'] = $path;
            $max['timestamp'] = $timestamp;
        }
    }

    return $max['path'];
}
于 2013-10-04T11:56:09.403 回答
0

您需要使用filemtime()功能:

<?php
// outputs e.g.  somefile.txt was last modified: December 29 2002 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
?>
于 2013-10-04T11:50:19.340 回答