首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用javascript/php自动搜索目录中的`<img图像[ src=` ]?

问如何使用javascript/php自动搜索目录中的`<img图像[ src=` ]?
EN

Stack Overflow用户
提问于 2015-08-09 19:44:11
回答 2查看 187关注 0票数 0

我不确定,但可以在目录中自动搜索图像src吗?

例如:

如果我的域名是:www.site.com

图片来源:files/images/demo.jpg

简单的方法:<img src="files/images/demo.jpg" />

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

我在努力,但没有效果。

EN

回答 2

Stack Overflow用户

发布于 2015-08-09 20:45:49

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

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

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

代码语言:javascript
复制
<?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);
?>

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

我希望这能帮到你。祝你编码愉快!

票数 1
EN

Stack Overflow用户

发布于 2015-08-09 20:59:21

如果我正确理解了问题,我认为你不能自动在目录中搜索图像src,因为如果你想在更高的级别上移动,你不必指定目录(例如,你在/files/images下,你只需使用../在/files下),相反,如果你想进入下一步,你必须指定文件夹(例如,你在/files下,你想在files/images/下,你必须指定images文件夹名,因为在files目录下可能有很多文件夹),所以没有像./或其他东西可以自动执行的标志。

希望这能回答你的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31903858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档