I have following code which give me list of all image in localhost :-
<?php
$target_dir = "uploadToper/";
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
$dir = "http://".$_SERVER['SERVER_NAME']."/apimerit/";
$collection = array();
/*
$collection[] = array('title' => 'sdfsdf', 'artist' =>'dsfsdf');
$collection[] = array('title' => 'dsfsdfs', 'artist' =>'dswdf');
$collection[] = array('title' => 'ssdfsf', 'artist' =>'dwer');
$collection[] = array('title' => 'sdsdfsd', 'artist' =>'dewrwedf');*/
foreach(glob($target_dir . '*.*') as $filename){
// echo $dir. $filename;
$imageFileType = pathinfo($filename,PATHINFO_EXTENSION);
$filename = explode("/",$filename)[1];
$collection[] = array('fileName' => $filename, 'fullPath'=>$dir.$target_dir. $filename, 'icon'=>$imageFileType);
}
echo json_encode($collection);
?>
I get this proper output:-
[{"fileName":"slide1.jpg","fullPath":"http:\/\/localhost\/apimerit\/uploadToper\/slide1.jpg","icon":"jpg"},{"fileName":"slide2.jpg","fullPath":"http:\/\/localhost\/apimerit\/uploadToper\/slide2.jpg","icon":"jpg"},{"fileName":"toper1_2016.jpg","fullPath":"http:\/\/localhost\/apimerit\/uploadToper\/toper1_2016.jpg","icon":"jpg"},{"fileName":"toper3_2015.jpg","fullPath":"http:\/\/localhost\/apimerit\/uploadToper\/toper3_2015.jpg","icon":"jpg"},{"fileName":"toper3_2017.jpg","fullPath":"http:\/\/localhost\/apimerit\/uploadToper\/toper3_2017.jpg","icon":"jpg"},{"fileName":"toper4_2017.jpg","fullPath":"http:\/\/localhost\/apimerit\/uploadToper\/toper4_2017.jpg","icon":"jpg"}]
but when I upload it ot my webserver on godady it giv following error:-
"Parse error: syntax error, unexpected '[' in G:\PleskVhosts\worldplanetesolutions.com\merithub2.worldplanetesolutions.com\apimerit\getToperImagesapi.php on line 15"
and if I remove "[1]" from "$filename = explode("/",$filename)[1];" it provides me an array of filename like this:-
[{"fileName":["uploadToper","slide1.jpg"],"fullPath":"http:/
/merithub2.worldplanetesolutions.com/apimerit/uploadToper/Array","icon":"jpg"},{"fileName":["uploadToper","slide2.jpg"],"fullPath":"http://merithub2.worldplanetesolutions.com/apimerit/uploadToper/Array","icon":"jpg"},{"fileName":["uploadToper","toper1_2016.jpg"],"fullPath":"http://merithub2.worldplanetesolutions.com/apimerit/uploadToper/Array","icon":"jpg"},{"fileName":["uploadToper","toper3_2015.jpg"],"fullPath":"http://merithub2.worldplanetesolutions.com/apimerit/uploadToper/Array","icon":"jpg"},{"fileName":["uploadToper","toper3_2017.jpg"],"fullPath":"http://merithub2.worldplanetesolutions.com/apimerit/uploadToper/Array","icon":"jpg"},{"fileName":["uploadToper","toper4_2017.jpg"],"fullPath":"http://merithub2.worldplanetesolutions.com/apimerit/uploadToper/Array","icon":"jpg"}]