函数查找文件夹并使用通配符

Function to look through folder and use a wildcard

本文关键字:通配符 查找 文件夹 函数      更新时间:2023-09-26

在Stack Overflow上潜伏了一段时间,我喜欢这个网站。

现在轮到我了。从下面的代码,我使背景图像随机每次页面加载。

有没有人能帮助我使这更有效,这样我就不用手动输入我的文件名了?我正在寻找某种通配符功能,可以通过我的给定文件夹和加载页脚*.png甚至*.png作为这个文件夹将只包含页脚模式。

var images = ['footer.png', 'footer2.png', 'footer3.png'];
$('#footer').css({'background-image': 'url(images/footers/' + images[Math.floor(Math.random() * images.length)] + ')'});

使用PHP,您可以这样做:

<?php
$directory = "./images/";
$images = glob($directory . "*.png");
foreach($images as $image)
{
  echo $image . "'n";
}
?>

只是不要让$directory成为一个任意的参数,因为俄罗斯黑客会劫持你的web应用。

要使用它,向PHP文件发出AJAX请求并解析输出,用'n字符分隔每个文件。

对于jQuery,

var images = [];
jQuery.load('images.php', function(data)
{
  images = data.split(''n');
});