CodeIgniter Forums
Directory Helper Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Directory Helper Question (/thread-64294.html)



Directory Helper Question - wolfgang1983 - 02-04-2016

I am using the codeIgniter directory helper but have small question.

When I display my images and directory's in my foreach loop $image variable is a directory it throws error.

Error Message: Array to string conversion I have attached image of error.

PHP Code:
array (size=5)
 
 0 => string 'codeigniter.png' (length=15)
 
 1 => string 'CSS-Tricks-1024x469.png' (length=23)
 
 2 => string 'IMG_20151230_154130.jpg' (length=23)
 
 3 => string 'preypay.png' (length=11)
 
 'subfolder\' => 
    array (size=0)
      empty 


As shown in var dump sub folder is directory but when I echo out the foreach loop it throws error for that directory.

Any suggestion I need to display sub directory and images. Images I can display fine

PHP Code:
<?php

class Filemanager extends CI_Controller {

    public function 
index() {

    
$this->load->helper('directory');

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

    
// Finds and loads images and directorys.
 
       
        $images 
directory_map('./images/catalog/'FALSETRUE);

 
       var_dump($images);

     
   foreach($images as $image) {

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

     
   }

 
       $this->load->view('filemanager_view'$data);

 
  }


View

PHP Code:
<div class="container" style="margin-top: 20px;">
<
div class="row">

<?
php if ($images) {?>

<?php foreach ($images as $image) {?>

    <div class="col-lg-3">
        <a class="thumbnail">
              <?php echo $image['img'];?>
        </a>
        <div class="caption text-center">
        </div>
      </div>

<?php }?>

<?php } else {?>

    <div class="col-lg-12 col-md-12 col-sm-12">
        <h3>No images available</h3>
    </div>

<?php }?>
</div>
</div> 



RE: Directory Helper Question - InsiteFX - 02-04-2016

Try casting $image ie; (string) $image etc;


RE: Directory Helper Question - keulu - 02-04-2016

that's because 'subfolder' key is not a string but an array. Smile

remove the key 'subfolder' with the unset() method from your array and everything is gonna be ok Smile

unset($images['subfolder']);


RE: Directory Helper Question - keulu - 02-04-2016

but if you have images in your subfolder, you can use a recursive method.

a recursive methods is a method who call itself


PHP Code:
$images_directory directory_map('./images/catalog/'FALSETRUE);
$images = array();

function 
getImages($images_directory){
    foreach (
$images_directory as $image_directory){
        if (
is_array($image_directory){
           
getImages($image_directory);
        }else{
            
$images[] = $image_directory;
        }
    }
}


getImages($images_directory);
var_dump($images); 


i'm not sure this methods work like that. i writted it from scratch. but this is how to do a recursive method.


RE: Directory Helper Question - wolfgang1983 - 02-04-2016

(02-04-2016, 09:42 AM)keulu Wrote: but if you have images in your subfolder, you can use a recursive method.

a recursive methods is a method who call itself


PHP Code:
$images_directory directory_map('./images/catalog/'FALSETRUE);
$images = array();

function 
getImages($images_directory){
 
   foreach ($images_directory as $image_directory){
 
       if (is_array($image_directory){
 
          getImages($image_directory);
 
       }else{
 
           $images[] = $image_directory;
 
       }
 
   }
}


getImages($images_directory);
var_dump($images); 


i'm not sure this methods work like that. i writted it from scratch. but this is how to do a recursive method.

I am just a bit confused will that let me still display the folders as well as images and there file names?


RE: Directory Helper Question - wolfgang1983 - 02-04-2016

(02-04-2016, 03:34 AM)InsiteFX Wrote: Try casting $image ie; (string) $image etc;

Seem to  have it working now by using scandir and array_diff

PHP Code:
<?php

class Filemanager extends CI_Controller {

    public function 
index() {

        
$this->load->helper('directory');

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

        
$directory scandir(FCPATH 'images/catalog'1);

        
$files array_diff($directory, array('.''..'));

 
       var_dump($files);

     
   foreach($files as $file) {

     
       $image FCPATH 'images/catalog/' $file;

     
       if (is_dir($image)) {
     
           
                $data
['images'][] = array(
         
           'test' => ''
         
       );

     
       } else {

             
   $data['images'][] = array(
             
       'test' => $file
                
);

            }

     
   }

 
       $this->load->view('filemanager_view'$data);
    }