使用PHP列出文件夹和子文件夹中的图像文件,并按创建日期对其排序
我有一个摄像头,每30秒上传一次图片到我ftp服务器上的文件夹中。 图像被放置到子文件夹中(例如2020->06->19) 我想用php将它们列在一个图库滑块中,最新的图像放在第一位 我在网上找到了这个代码, 不幸的是,我在php方面的经验不是很好。 代码对我来说很有用,但我只需要一个选项,它可以按日期对图像进行排序(首先是最新的图像) 有人能帮我吗 谢谢使用PHP列出文件夹和子文件夹中的图像文件,并按创建日期对其排序,php,Php,我有一个摄像头,每30秒上传一次图片到我ftp服务器上的文件夹中。 图像被放置到子文件夹中(例如2020->06->19) 我想用php将它们列在一个图库滑块中,最新的图像放在第一位 我在网上找到了这个代码, 不幸的是,我在php方面的经验不是很好。 代码对我来说很有用,但我只需要一个选项,它可以按日期对图像进行排序(首先是最新的图像) 有人能帮我吗 谢谢 请查看这是否适合您 <?php // file name: list_pics.php global $startDir; $fi
请查看这是否适合您
<?php
// file name: list_pics.php
global $startDir;
$fileList = [];
/**
* List Directories function, which transverses sub-folders
* listing out the image files that exists within it.
*/
function listDir( $path ) {
global $startDir;
$handle = opendir( $path );
global $fileList;
while (false !== ($file = readdir($handle))) {
if( substr( $file, 0, 1 ) != '.' ) {
if( is_dir( $path.'/'.$file ) ) {
listDir( $path.'/'.$file );
}
else {
if( !empty(@getimagesize( $path.'/'.$file ))) {
array_push($fileList, $path.'/'.$file);
}
}
}
}
closedir( $handle );
}
function createLinks($fileList) {
foreach ($fileList as $filePath) {
/*
// Uncomment if using with the below "pic.php" script to
// encode the filename and protect from direct linking.
$url = 'http://domain.tld/images/pic.php?pic='
.urlencode( str_rot13( substr( $path, strlen( $startDir )+1 ).'/'.$file ) );
*/
$url='http://domain.tld/images/'.
substr( $filePath, strlen( $startDir )+1 ).'/'.$file;
// You can customize the output to use img tag instead.
echo "<a href='".$url."'>".$url."</a><br>";
}
}
$startDir = '.';
listDir( $startDir );
createLinks($fileList);
看看这个。你需要两个循环。一个是获取数组中的所有图像,然后对它们进行排序,并使用排序后的数组生成HTML。嗨,Sameer!谢谢你的帮助。不幸的是,我真的不知道如何做到这一点,我的php技术不是很好:-(我有点想寻找一些“已经存在的”。如果解决方案对你有效,请接受答案。谢谢!