如何知道整个网站的图像的所有文件名?

时间:2014-04-25 10:17:52

标签: javascript php jquery

我希望将整个网站的所有图像都放在一个数组中,并且我的所有图像都在一个名为 images 的文件夹中,并且在该文件夹中有很多子文件夹,所以现在我怎么能得到所有的图像'数组中的路径?

是否可以使用jquery,javascript?如果没有,php就可以。

3 个答案:

答案 0 :(得分:0)

试试这个PHP脚本:

$dir = "images/*/";

//get all files with .jpg
$images = glob("" . $dir . "*.jpg");

$imgs = array();
// store images in $imgs - after that optional display
foreach($images as $image) {
    $imgs[] = $image;
}

//display
foreach ($imgs as $img) {
    echo '<img src="'.$img.'" />';
}

存储在JS数组中:

$JSarray = json_encode($imgs);
echo "var JSarray = ". $JSarray . ";\n";

答案 1 :(得分:0)

我发现这个递归函数可以为您提供给定目录的树,您可以使用$ exclude参数仅过滤图像文件

<?php
/**
     * Get an array that represents directory tree
     * @param string $directory     Directory path
     * @param bool $recursive         Include sub directories
     * @param bool $listDirs         Include directories on listing
     * @param bool $listFiles         Include files on listing
     * @param regex $exclude         Exclude paths that matches this regex
     */
    function directoryToArray($directory, $recursive = true, $listDirs = false, $listFiles = true, $exclude = '') {
        $arrayItems = array();
        $skipByExclude = false;
        $handle = opendir($directory);
        if ($handle) {
            while (false !== ($file = readdir($handle))) {
            preg_match("/(^(([\.]){1,2})$|(\.(svn|git|md))|(Thumbs\.db|\.DS_STORE))$/iu", $file, $skip);
            if($exclude){
                preg_match($exclude, $file, $skipByExclude);
            }
            if (!$skip && !$skipByExclude) {
                if (is_dir($directory. DIRECTORY_SEPARATOR . $file)) {
                    if($recursive) {
                        $arrayItems = array_merge($arrayItems, directoryToArray($directory. DIRECTORY_SEPARATOR . $file, $recursive, $listDirs, $listFiles, $exclude));
                    }
                    if($listDirs){
                        $file = $directory . DIRECTORY_SEPARATOR . $file;
                        $arrayItems[] = $file;
                    }
                } else {
                    if($listFiles){
                        $file = $directory . DIRECTORY_SEPARATOR . $file;
                        $arrayItems[] = $file;
                    }
                }
            }
        }
        closedir($handle);
        }
        return $arrayItems;
    }
?>

source

关于jQuery的方式,你不能直接访问目录,你必须为你的后端提供一个ajax调用列表,然后给你一个json对象,你用jQuery渲染它

答案 2 :(得分:0)

使用RecursiveDirectoryIteratorRecursiveIteratorIteratorRegexIterator来实现此目标。

$imagesExtensions = array('jpg','png','gif'); //<=-- Add your extensions
$extensions = implode('|',$imagesExtensions);
$directory  = new RecursiveDirectoryIterator('images');
$iterator   = new RecursiveIteratorIterator($directory);//RecursiveDirectoryIterator::SKIP_DOTS);
$regex      = new RegexIterator($iterator, "/^.+\.$extensions$/i", RecursiveRegexIterator::GET_MATCH);
foreach ($regex as $filename=>$object) {
    echo $filename . '<br>';
    $images[] = $filename; //<----- All images are stored in this array sequentially
}

<强> OUTPUT :

images\google\client\ext\resources\themes\images\access\boundlist\trigger-arrow.png
images\google\client\ext\resources\themes\images\access\box\corners-blue.gif
images\google\client\ext\resources\themes\images\access\box\corners.gif
//....... Goes on..