Welcome Guest, Not a member yet? Register   Sign In
Directory Helper Question
#1

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> 


Attached Files Thumbnail(s)
   
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

Try casting $image ie; (string) $image etc;
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 02-04-2016, 06:26 AM by keulu.)

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']);
Reply
#4

(This post was last modified: 02-04-2016, 09:56 AM by keulu.)

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.
Reply
#5

(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?
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#6

(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);
    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB