【发布时间】:2020-06-19 17:51:54
【问题描述】:
我确实有一个网络摄像头,它每 30 秒将图像上传到我的 ftp 服务器上的文件夹中。 图像被放置在子文件夹中(例如 2020 -> 06 -> 19)。
我想用 php 将它们列出到画廊滑块中 - 最新的图像首先放置在其中。
我在网上找到了这段代码, 不幸的是,我在 php 方面的经验并不是很好。 该代码对我有用,但我只需要一个选项来按日期对图像进行排序(首先是最新图像)。
有人可以帮我吗?
谢谢!!
<?php
// file name: list_pics.php
global $startDir;
/**
* List Directories function, which transverses sub-folders
* listing out the image files that exists within it.
*/
function listDir( $path ) {
global $startDir;
$handle = opendir( $path );
while (false !== ($file = readdir($handle))) {
if( substr( $file, 0, 1 ) != '.' ) {
if( is_dir( $path.'/'.$file ) ) {
listDir( $path.'/'.$file );
}
else {
if( @getimagesize( $path.'/'.$file ) ) {
/*
// 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( $path, strlen( $startDir )+1 ).'/'.$file;
// You can customize the output to use img tag instead.
echo "<a href='".$url."'>".$url."</a><br>";
}
}
}
}
closedir( $handle );
} // End listDir function
$startDir = '.';
listDir( $startDir );
?>
【问题讨论】:
-
见stackoverflow.com/questions/2667065/sort-files-by-date-in-php。你需要两个 for 循环。一种是获取数组中的所有图像,然后对它们进行排序并使用排序后的数组生成 HTML。
-
嗨萨米尔!感谢您的帮助。不幸的是,我真的不知道该怎么做,我在 php 方面的专业知识不是很好:-( 我有点在寻找“已经存在”的东西。
-
如果解决方案对您有效,请接受答案。谢谢!
标签: php