Shaheen Khan Shaheen Khan - 4 months ago 24
Android Question

Php script to get url of all the images in directory in json string

I already have a android app built that shows images from the server when it calls a json file which contains all the url of images.

so i want a php script to search for all the images in particular directory and then put url of all the images in json format.

my domain http://arture.16mb.com

my images dir http://arture.16mb.com/images/

json format i currently use

[{
"name": "Deadpool",

"url": {

"small": "xyzdomain/images/1.jpg",

"medium": "xyzdomain/images/1.jpg",

"large": "xyzdomain/images/1.jpg"
},

"timestamp": "February 12, 2016"

},

{

"name": "Batman vs Superman",

"url": {

"small": "xyzdomain/images/1.jpg",

"medium": "xyzdomain/images/1.jpg",

"large": "xyzdomain/images/1.jpg"

},

"timestamp": "March 25, 2016"

}]

json format which i want my php script to make

"image": {
"url": "//mydomain/images/....jpeg"

},

{

"url": "//mydomain/images/....jpeg"

},

{

"url": "//mydomain/images/....jpeg"

}

Answer

You may try this:

$json = "";

foreach (glob('images/*.png') as $file){

    $json .= '{ "url":"//mydomain/'.$file.'" }, ';
}

$json = trim($json, ", ");

What it does, is gets all the files with .png extention, if you want jpeg files you can use *.jpeg Then, all the filenames are appended to $json