我想为我的图片文件夹(目录)构建具有以下功能的API服务:
但我想使用网址查询每页显示20张图片网址。
反向排序。
示例:
我的目录中有这些图片
我希望API网址和数据项看起来像这样
mypage.com/images-json?page=1
{
"img1": "/images/path41.jpg",
"img2": "/images/path40.jpg",
"img3": "/images/path39.jpg",
"img4": "/images/path38.jpg",
"img5": "/images/path37.jpg",
"img6": "/images/path36.jpg",
"img7": "/images/path35.jpg",
"img8": "/images/path34.jpg",
"img9": "/images/path33.jpg",
"img10": "/images/path32.jpg",
"img11": "/images/path31.jpg",
"img12": "/images/path30.jpg",
"img13": "/images/path29.jpg",
"img14": "/images/path28.jpg",
"img15": "/images/path27.jpg",
"img16": "/images/path26.jpg",
"img17": "/images/path25.jpg",
"img18": "/images/path24.jpg",
"img19": "/images/path23.jpg",
"img20": "/images/path22.jpg"
}
mypage.com/images-json?page=2
{
"img1": "/images/path21.jpg",
"img2": "/images/path20.jpg",
"img3": "/images/path19.jpg",
"img4": "/images/path18.jpg",
"img5": "/images/path17.jpg",
"img6": "/images/path16.jpg",
"img7": "/images/path15.jpg",
"img8": "/images/path14.jpg",
"img9": "/images/path13.jpg",
"img10": "/images/path12.jpg",
"img11": "/images/path11.jpg",
"img12": "/images/path10.jpg",
"img13": "/images/path9.jpg",
"img14": "/images/path8.jpg",
"img15": "/images/path7.jpg",
"img16": "/images/path6.jpg",
"img17": "/images/path5.jpg",
"img18": "/images/path4.jpg",
"img19": "/images/path3.jpg",
"img20": "/images/path2.jpg"
}
mypage.com/images-json?page=3
{
"img1": "/images/path1.jpg",
"img2": "/images/path.jpg"
}
目前我只有这个代码,但是这段代码只显示我目录文件夹中的所有图片,排序也不反转:
<?php
$this->load->helper('directory'); //load directory helper
$dir = "images"; // Your Path to folder
$map = directory_map($dir); /* This function reads the directory path specified in the first parameter and builds an array representation of it and all its contained files. */
foreach ($map as $k)
{?>
<img src="<?php echo base_url($dir)."/".$k;?>" alt="">
<?php }
?>
答案 0 :(得分:0)
尝试对目录网址使用FC_PATH,因为base_url不适用于目录数据。所以希望它能帮到你。
<?php
$this->load->helper('directory'); //load directory helper
$dir = FCPATH."images/"; // Your Path to folder
$map = directory_map($dir); /* This function reads the directory path specified in the first parameter and builds an array representation of it and all its contained files. */
//and for sorting may be use krsort for this .
krsort($map);
foreach ($map as $k)
{?>
<img src="<?php echo $dir.$k;?>" alt="">
<?php }
?>