我不确定,但可以自动搜索图像 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/