Jump to content

simple/quick help to mod this script (check if dir is empty)


xl97

Recommended Posts

I am currently using this script:
[code=php:0]
<?
$startDir=$_POST["startDir"];
//$startDir='.';
$exDir="images";
function scandir($dir) {
global $exDir;
        if($handle = opendir($dir)) {     
        while(($file = readdir($handle)) !== false){
                        if($file != '.' && $file != '..' && $file != $exDir){
                                if(is_dir("$dir/$file")){
                    print "<directory label='$file' parent='$dir/$file'>";
                          scandir("$dir/$file");
                          print "</directory>";
                }else {
                        print "<file label='$file' path='$dir/$file' />";
}
}
        }
closedir($handle);
        }
}
scandir($startDir);
?>
[/code]

it works great and does everything it is supposed to.

However it outputs the xml/string like so:


<file label='testScrape7.php' path='./testScrape7.php' /><file label='testScrape8.php' path='./testScrape8.php' />[b]<directory label='empty' parent='./empty'></directory>[/b]


notice the part in BOLD.. when it encounters a directory.. I need to add a simple 'check' (if/else) statement to see if the directory is EMPTY.. is so.. I need to print/echo a differnt 'string' than the one I am doing now (if there IS content in the directory)

I need it to be like this:
<file label='testScrape7.php' path='./testScrape7.php' /><file label='testScrape8.php' path='./testScrape8.php' />[b]<directory label='emptyDir' parent='./empty'><empty label='EMPTY' /></directory>[/b]

I dont know.. so somehow I need the equivelent of this:


<?
$startDir=$_POST["startDir"];
//$startDir='.';
$exDir="images";
function scandir($dir) {
global $exDir;
        if($handle = opendir($dir)) {     
        while(($file = readdir($handle)) !== false){
                        if($file != '.' && $file != '..' && $file != $exDir){
                                [size=10pt][b]if(is_dir("$dir/$file")){
    if("$dir/$file" == EMPTY DIRECTORY){
        print "<directory label='$file' parent='$dir/$file'>";
        print "<empty label='EMPTY' />";
        print "</directory>";
    }else{
        print "<directory label='$file' parent='$dir/$file'>";
        scandir("$dir/$file");
        print "</directory>";
    }
}else {
    print "<file label='$file' path='$dir/$file' />";
} [/b][/size]
}
        }
closedir($handle);
        }
}
scandir($startDir);
?>


can some help me out here on this quick one?  Im not sure how I can quickly add or call a function/routine to check exactly like I am proposing in the 'mock' script above.


p.s.:
Also... I was wondering what other data could I grab from the files as I scanning the directories??  can I grab file size as well?? and pass that back as an attribute too?? (as in 'easily' in my current method of doing things above?) 




Thanks  :)
Here is a way to check if a directory is empty..

[code]<?php
$directory = "/path/to/dir/";
$handle = opendir($directory);

while (FALSE !== ($item = readdir($handle)))
{
if($item != '.' && $item != '..')
{
echo "Directory is not empty";
die();
}
else {
echo "Directory is empty";
die();
}
}
?>[/code]
Here is actually a better way.. I cannot help you put it in your function.

[code]<?php

$globaldir = "/path/to/files/";

$handle = opendir("$globaldir");
  while ($file = readdir($handle)) {
      if ($file != "." && $file != "..") {
          if (!empty($file)) {
          echo "$file: ".filesize("$globaldir$file")." bytes<br />";
          } else { echo "Empty"; }
}
}
closedir($handle);

$dirname = "$globaldir";

function test($dirname){

  $result=false;
  if(is_dir($dirname) ){
      $result=true;
      $handle = opendir($dirname);
      while( ( $name = readdir($handle)) !== false){
              if ($name!= "." && $name !=".."){
            $result=false;
            break;
          }
      }
      closedir($handle);
  }
  if ($result == "1") { echo "Directory is empty"; }
}

echo test($dirname);

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.