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

标签 javascript php html 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);
?>

如果您有任何疑问,请在下面发表评论,我会尽快回复您。

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

关于javascript - 如何使用 javascript/php 自动搜索 `<img src=` [image] 的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31903858/

相关文章:

javascript - 如何将 KnockoutJS 可观察结果打印到控制台?

javascript - 如何将本地存储中的值存储在数组中,JavaScript?

php - 从 heroku php 服务器通过 firebase 发送 android 通知

php - OAuth 重定向后 session 丢失

javascript - 如果CDN失败,不使用document.write(),如何加载本地静态文件?

html - 当我添加 <DIV> 时出现底部滚动条,即使它不大于页面宽度

javascript - 无法在另一个 js 文件中加载自定义 Mongoose 架构

javascript - 更改 ASP.NET MVC 5 中 Controller ActionResult 的 html 元素样式

php - 通过 php 上传到本地驱动器上的文件夹时出现权限问题

javascript - 如何从 html 格式的文本创建元素?