[英]php - Get the last modified dir
A little stuck on this and hoping for some help. 有点坚持这一点,并希望得到一些帮助。 I'm trying to get the last modified dir from a path in a string. 我正在尝试从字符串中的路径获取最后修改的目录。 I know there is a function called " is_dir " and I've done some research but can't seem to get anything to work. 我知道有一个名为“ is_dir ”的函数,我做了一些研究,但似乎无法得到任何工作。
I don't have any code i'm sorry. 我没有任何代码,我很抱歉。
<?php
$path = '../../images/';
// echo out the last modified dir from inside the "images" folder
?>
For example: The path variable above has 5 sub folders inside the "images" dir currently right now. 例如:上面的路径变量目前在“images”目录中有5个子文件夹。 I want to echo out "sub5" - which is the last modified folder. 我想要回显“sub5” - 这是最后修改过的文件夹。
You can use
scandir()
instead ofis_dir()
function to do it. 您可以使用scandir()
而不是is_dir()
函数来执行此操作。
Here is an example. 这是一个例子。
function GetFilesAndFolder($Directory) {
/*Which file want to be escaped, Just add to this array*/
$EscapedFiles = [
'.',
'..'
];
$FilesAndFolders = [];
/*Scan Files and Directory*/
$FilesAndDirectoryList = scandir($Directory);
foreach ($FilesAndDirectoryList as $SingleFile) {
if (in_array($SingleFile, $EscapedFiles)){
continue;
}
/*Store the Files with Modification Time to an Array*/
$FilesAndFolders[$SingleFile] = filemtime($Directory . '/' . $SingleFile);
}
/*Sort the result as your needs*/
arsort($FilesAndFolders);
$FilesAndFolders = array_keys($FilesAndFolders);
return ($FilesAndFolders) ? $FilesAndFolders : false;
}
$data = GetFilesAndFolder('../../images/');
var_dump($data);
From above example the last modified
Files
orFolders
will show as Ascending order. 从上面的示例中,最后修改的Files
或Folders
将显示为升序。
You can also separate your files and folder by checking is_dir()
function and store the result in 2 different arrays like $FilesArray=[]
and $FolderArray=[]
. 您还可以通过检查is_dir()
函数来分隔文件和文件夹,并将结果存储在2个不同的数组中,如$FilesArray=[]
和$FolderArray=[]
。
Details about filemtime() scandir() arsort() 有关filemtime()的详细信息scandir() arsort()
Here's one way you can accomplish this: 这是您可以实现此目标的一种方法:
<?php
// Get an array of all files in the current directory.
// Edit to use whatever location you need
$dir = scandir(__DIR__);
$newest_file = null;
$mdate = null;
// Loop over files in directory and if it is a subdirectory and
// its modified time is greater than $mdate, set that as the current
// file.
foreach ($dir as $file) {
// Skip current directory and parent directory
if ($file == '.' || $file == '..') {
continue;
}
if (is_dir(__DIR__.'/'.$file)) {
if (filemtime(__DIR__.'/'.$file) > $mdate) {
$newest_file = __DIR__.'/'.$file;
$mdate = filemtime(__DIR__.'/'.$file);
}
}
}
echo $newest_file;
This will work too just like the other answers. 这将像其他答案一样工作。 Thanks everyone for the help! 谢谢大家的帮助!
<?php
// get the last created/modified directory
$path = "images/";
$latest_ctime = 0;
$latest_dir = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
if(is_dir($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_dir = $entry;
}
} //end loop
echo $latest_dir;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.