PHP Scandir返回额外的句点 [英] PHP Scandir returns extra periods
本文介绍了PHP Scandir返回额外的句点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我试图构建一个脚本来扫描目录并返回随机图像以用作背景.
So I am trying to build a script that scans a directory and returns random images to be used as backgrounds.
php看起来像这样:
The php looks like this:
$dir = "views/img/bg/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
$random_key = array_rand($files, 1);
$random = $files[$random_key];
然后我只是使用一些简单的jquery将图像附加为背景:
Then I am just using some simple jquery to attach the images as backgrounds:
<script>
$(document).ready(function(){
$("body").css( "background" , "url(http://'.$url_root.'/views/img/bg/'.$random.'), center center" );
});
</script>
一切正常,但背景文件夹中所有图像的数组似乎返回诸如."之类的东西.或"..",而不是偶尔使用图像名称.我不确定发生了什么-有什么想法吗?
Everything works fine but the array of all the images in the background folder seems to be returning stuff like '.' or '..' instead of image names every once in a while. Im not sure what is going on - any ideas?
推荐答案
'.和'..'返回当前目录和父目录.您可以过滤它们:
'.' and '..' are returned for current and parent directory. You can filter them:
while (false !== ($filename = readdir($dh))) {
if ($filename != '.' && $filename != '..')
$files[] = $filename;
}
这篇关于PHP Scandir返回额外的句点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文