本文介绍了从目录中获取图像URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery制作幻灯片,为此我尝试将所有图像的网址存储在一个文件夹中,而不必手动编写它们
看起来我必须玩Ajax ,但我有点困惑
我基本上想在我的PHP代码中将var存储在一个数组中
I'm trying to make a slideshow with jQuery, for this I try to get all of the images' urls stored in a folder without having to write them manualyIt looks like I have to play around with Ajax, but I'm a bit confusedI would basically like to get the var stored in an array in my PHP code
<?php
$dir = "img";
if (is_dir($dir)){
if ($dh = opendir($dir)){
$images = array();
while (($file = readdir($dh)) !== false){
if (!is_dir($dir.$file)) $images[] = $file;
}
closedir($dh);
}
}
$max = count($images);
那我怎么能快速得到 $ images []的价值在javascript中code>?非常感谢帮助! :)
So how could I quickly get the values of $images[]
in javascript? Anly help would be very appreciated! :)
推荐答案
<?php
$dir = "img";
$images = array();
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
if (!is_dir($dir.$file)) $images[] = $file;
}
closedir($dh);
}
}
header('Content-Type: application/json');
echo json_encode($images);
只要设置了标题内容类型,jQuery就会自动解析JSON:
jQuery will automatically parse the JSON as long as the header content-type is set:
$.ajax({
'url' : 'imagelist.php',
'success': function(result) {
...
},
});
这篇关于从目录中获取图像URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!