如何使用javascript/php自动搜索目录中的'<img src=' [image]?



我不确定,但是否可以自动搜索图像目录src?
例如:
如果我的域名:www.site.com
图片来源:files/images/demo.jpg
简单方法:<img src="files/images/demo.jpg" />

有可能这样做:<img src="././demo.jpg" /> ?

您需要将目录变量设置为您希望扫描的目录路径。

我在php中添加了注释,这样你就可以一步一步地看到发生了什么。

你可以使用ajax调用php文件来返回该目录下的所有名称…这取决于您希望如何将其实现到现有的源代码中。我已经运行了这个,并检查了它的任何错误,一切似乎都很好。

<?php
$directory='images/';
//Scan the directory (returns array of files)
$files = scandir($directory);
//Remove "." and ".." from the files array 
unset($files[0],$files[1]);
//Create an array to hold the names.
$Images = array();
//Loop through each file found in the directory scan.
    foreach ($files as &$f) {
$docs=$directory.$f; 
//Get file information. (We want to check the extension)
$info=pathinfo($docs);
//Make the extension lowsercase before running the if condition.
$format=strtolower($info['extension']);
    //Check for allowed formats 
    if(($format=="jpg")||($format=="png")||($format=="gif")){
    //Places image name into the array if the format is a match
        $Images[]=$f;
    }
}
//Print the array of images. 
    print_r($Images);
?>

如果你有任何问题,请在下面留言,我会尽快回复你。

我希望这对你有帮助。编码快乐!

如果我正确地理解了这个问题,我认为你不能自动搜索目录的图像src,因为如果你想在水平上移动,你不必指定目录(例如,你在/files/images下,你只需要使用..//files下),相反,如果你想往下走,你必须指定文件夹(例如,你在/files下,你想在files/images/下,你必须指定images文件夹名称,因为files目录下可能有许多文件夹),所以没有像./这样的标志,或者其他东西可以自动完成。

相关内容

最新更新