Hermes Djohar
Hermes Djohar

Reputation: 121

PHP CodeIgniter - How to count images inside folders and their respective subfolders

How to count total numbers of images inside our folders and subfolders?

I put this in my view. Well, as MVC way, this may look sucks to be in view. I know how to put this into models, but don't know how to call it with controllers and views

<?php
$img = count(glob("./assets/images/*.*"));
$about = count(glob("./assets/images/aboutus/*.*"));
$blog1 = count(glob("./assets/images/blog/*.*"));
$mason = count(glob("./assets/images/blog/masonary/*.*"));
$tl = count(glob("./assets/images/blog/timeline/*.*"));
$blog2 = count(glob("./assets/images/blogdetails/*.*"));
$gallery = count(glob("./assets/images/gallery/*.*"));
$home = count(glob("./assets/images/home/*.*"));
$home2 = count(glob("./assets/images/home/slider/*.*"));
$ico = count(glob("./assets/images/ico/*.*"));
$lb = count(glob("./assets/images/lightbox/*.*"));
$keg = count(glob("./assets/images/kegiatan/*.*"));
$port1 = count(glob("./assets/images/portfolio/*.*"));
$port2 = count(glob("./assets/images/portfolio-details/*.*"));
$leader = count(glob("./assets/images/leaders/*.*"));
$srv = count(glob("./assets/images/services/*.*"));
$usr = count(glob("./assets/images/users/*.*"));
$count = $img+$about+$blog1+$mason+$tl+$blog2+$gallery+$home+$home2+$ico+$lb+$keg+$port1+$port2+$leader+$srv+$usr;
?>

Output:

<div class="col-sm-3 text-center wow bounceIn" data-wow-duration="1000ms" data-wow-delay="300ms">
  <h1 class="timer bold" data-to="<?= $count;?>" data-speed="3000" data-from="0"></h1> 
    <h3>Total Images</h3>
 </div>

Is there a way to make this simple?

Upvotes: 0

Views: 568

Answers (2)

ourmandave
ourmandave

Reputation: 1585

Format to call a method in a model from a controller.

Model file:

application/models/Photo_shoot_model.php


class Photo_shoot_model extends CI_Model {

public function image_count() {

$img = count(glob("./assets/images/*.*"));
$about = count(glob("./assets/images/aboutus/*.*"));
$blog1 = count(glob("./assets/images/blog/*.*"));
$mason = count(glob("./assets/images/blog/masonary/*.*"));
$tl = count(glob("./assets/images/blog/timeline/*.*"));
$blog2 = count(glob("./assets/images/blogdetails/*.*"));
$gallery = count(glob("./assets/images/gallery/*.*"));
$home = count(glob("./assets/images/home/*.*"));
$home2 = count(glob("./assets/images/home/slider/*.*"));
$ico = count(glob("./assets/images/ico/*.*"));
$lb = count(glob("./assets/images/lightbox/*.*"));
$keg = count(glob("./assets/images/kegiatan/*.*"));
$port1 = count(glob("./assets/images/portfolio/*.*"));
$port2 = count(glob("./assets/images/portfolio-details/*.*"));
$leader = count(glob("./assets/images/leaders/*.*"));
$srv = count(glob("./assets/images/services/*.*"));
$usr = count(glob("./assets/images/users/*.*"));
$count = $img+$about+$blog1+$mason+$tl+$blog2+$gallery+$home+$home2+$ico+$lb+$keg+$port1+$port2+$leader+$srv+$usr;

return $count;

}

}

In controller:

public function index() {

// load model
$this->load->model('photo_shoot_model');

// call method
$howManyImages = $this->photo_shoot_model->image_count()

}

All about models: https://codeigniter.com/user_guide/general/models.html

But really, I'd use the RecursiveDirectoryIterator mentioned by @MayurVirkar.

Upvotes: 0

MayurVirkar
MayurVirkar

Reputation: 51

Please take a look at https://stackoverflow.com/a/10895775/7089527

You could do it like this using the [RecursiveDirectoryIterator][1]

<?php
function scan_dir($path){
  $ite=new RecursiveDirectoryIterator($path);

  $bytestotal=0;
  $nbfiles=0;
  foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {
      $filesize=$cur->getSize();
      $bytestotal+=$filesize;
      $nbfiles++;
      $files[] = $filename;
  }

  $bytestotal=number_format($bytestotal);

  return array('total_files'=>$nbfiles,
'total_size'=>$bytestotal,'files'=>$files);
   }

$files = scan_dir('./');

echo "Total: {$files['total_files']} files, {$files['total_size']} >bytes\n";
//Total: 1195 files, 357,374,878 bytes 
?>

[1]: http://php.net/manual/en/class.recursivedirectoryiterator.php

Hope it helps

Upvotes: 1

Related Questions