Link to home
Start Free TrialLog in
Avatar of egovernment
egovernmentFlag for Afghanistan

asked on

Calculate all files php script

I search a script develop it with php to calculate or found total numbers of files avilable on my web site hosting including sub folders also.
Avatar of bartvd
bartvd
Flag of Netherlands image

You could use this function from http://php.net/manual/en/function.readdir.php

Then use count($foo); for the number of files.


Get all files on recursive directories in single array.

<?php
/*
 * mrlemonade ~
 */

function getFilesFromDir($dir) {

  $files = array();
  if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            if(is_dir($dir.'/'.$file)) {
                $dir2 = $dir.'/'.$file;
                $files[] = getFilesFromDir($dir2);
            }
            else {
              $files[] = $dir.'/'.$file;
            }
        }
    }
    closedir($handle);
  }

  return array_flat($files);
}

function array_flat($array) {

  foreach($array as $a) {
    if(is_array($a)) {
      $tmp = array_merge($tmp, array_flat($a));
    }
    else {
      $tmp[] = $a;
    }
  }

  return $tmp;
}

// Usage
$dir = '/data';
$foo = getFilesFromDir($dir);

print_r($foo);
?>

Open in new window

Avatar of egovernment

ASKER

Hi bartvd

This script is not work

only give me this message

Get all files on recursive directories in single array
You need to fill in a path to the files. In this example all files in the directory of the script and all subfolders are counted.


<?php
/*
 * mrlemonade ~
 */

function getFilesFromDir($dir) {

  $files = array();
  if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            if(is_dir($dir.'/'.$file)) {
                $dir2 = $dir.'/'.$file;
                $files[] = getFilesFromDir($dir2);
            }
            else {
              $files[] = $dir.'/'.$file;
            }
        }
    }
    closedir($handle);
  }

  return array_flat($files);
}

function array_flat($array) {

  foreach($array as $a) {
    if(is_array($a)) {
      $tmp = array_merge($tmp, array_flat($a));
    }
    else {
      $tmp[] = $a;
    }
  }

  return $tmp;
}

// Usage
$dir = dirname(__FILE__);
$foo = getFilesFromDir($dir);
$counter=0;
foreach($foo as $file)
{
if($file!="")
{
//echo "$file<br />";
$counter++;
}
}
echo "Number of files: ";
echo $counter;
?>

Open in new window

The above script going subfolder deeply ?

That's meaning when enter subfolder should also going inside subfolders until finish return to the same level ?
Yes, it does.
@egovernment: From this question and some others, it appears to me that you need to get some foundation in how PHP works and what the correct syntax is.  Please read this tutorial, then buy this book and give yourself a few weeks to work through the learning process. It will not make you a pro, but it will be interesting and enjoyable, and you will come out of this brief education process way ahead in your current PHP knowledge.

Tutorial: http://php.net/tut.php
Book: http://www.sitepoint.com/books/phpmysql4/

As a sidebar note, I would add that some of our questions here at EE come from people who have tried to learn PHP by using Dreamweaver scripts.  Don't do that.  Dreamweaver creates some of the worst PHP code ever written.  Stick to the examples and ideas in the SitePoint book for now.  Once you master that, you can go on to higher levels of PHP understanding with this book:

http://www.amazon.com/PHP-5-Practice-Elliott-White/dp/0672328887

Best of luck with your project, ~Ray
ASKER CERTIFIED SOLUTION
Avatar of Sandeep Kothari
Sandeep Kothari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial