public function selectImages() { $this->render("/Layout/ajax"); //this line render the view, since this is a ajax response it will load the respective ajax view if ($this->request->is('post')) { // $dir represents the /img folder where all the images are stored, this means that // this new folder command is not creating a new folder, just creating a new path $dir = new folder(WWW_ROOT . 'img/'); //$path = $dir->path; // ->find('.*', true) => finds all the files inside /img folder $files = $dir->find('.*', true); // encode the files in the json format echo json_encode($files); exit; // it doesn't work without the exit; } }