使用URL查询构建API服务,每页20个数据项(codeigniter PHP)

I want to build API service for my images folder (directory) with features:

  1. But I want use url query to show 20 images URL per page.

  2. And reverse sorting.

example:

I have this images on my directory

  1. /images/path.jpg
  2. /images/path1.jpg
  3. /images/path2.jpg
  4. /images/path3.jpg
  5. /images/path4.jpg
  6. /images/path5.jpg
  7. /images/path6.jpg
  8. /images/path7.jpg
  9. /images/path8.jpg 10./images/path9.jpg
  10. /images/path10.jpg
  11. /images/path11.jpg
  12. /images/path12.jpg
  13. /images/path13.jpg
  14. /images/path14.jpg
  15. /images/path15.jpg
  16. /images/path16.jpg
  17. /images/path17.jpg
  18. /images/path18.jpg
  19. /images/path19.jpg
  20. /images/path20.jpg
  21. /images/path21.jpg
  22. /images/path22.jpg
  23. /images/path23.jpg
  24. /images/path24.jpg
  25. /images/path25.jpg
  26. /images/path26.jpg
  27. /images/path27.jpg
  28. /images/path28.jpg
  29. /images/path29.jpg
  30. /images/path30.jpg
  31. /images/path31.jpg
  32. /images/path32.jpg
  33. /images/path33.jpg
  34. /images/path34.jpg
  35. /images/path35.jpg
  36. /images/path36.jpg
  37. /images/path37.jpg
  38. /images/path38.jpg
  39. /images/path39.jpg
  40. /images/path40.jpg
  41. /images/path41.jpg

I want the API url and data item look like this

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"
}

Currently I only have this code, but this code only will show all of images from my directory folder, sorting also not reverse:

<?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 }

?> 

try to use FC_PATH for Directory Url , because base_url is not worked for directory data.so try it hope it will helps you .

<?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 }
?>