CodeIgniter Forums

Full Version: Is_dir and Is_file inside foreach loop error.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am getting error in my foreach loop using

Array to string conversion error. On the line where is_dir and is_file.

Question: Where have I gone wrong? How to fix it?

Seems to be if any files in sub folder throws the error that's what causing error.


PHP Code:
foreach ($files as $image) {

if (
is_dir($directory $image)) {

$data['images'][] = array(
'name' => $image
);

} elseif (
is_file($directory $image)) {
$data['images'][] = array(
'name' => $image
);




Controller function


PHP Code:
public function index() {
$userdata $this->user_model->getuser($this->user_model->getuserid());

if (
$this->input->get('directory')) {
$directory FCPATH 'uploads/' $userdata['upload_folder'] .'/'$this->input->get('directory') .'/';
} else {
$directory FCPATH 'uploads/' $userdata['upload_folder'] .'/';
}

$files = array();

$files directory_map($directoryFALSETRUE);

   $data['images'] = array();

foreach (
$files as $image) {

if (
is_dir($directory $image)) {

$data['images'][] = array(
'name' => $image
);

} elseif (
is_file($directory $image)) {
$data['images'][] = array(
'name' => $image
);

}

$this->load->view('extension/filemanager'$data);


Perhaps your directory map is returning an array that is more complex than you have allowed for, so your $image is not a filename but an array of filenames. If you output the files array you generated with a var_dump what do you get?