Jump to content

Directory List script help for n00b


mike760534211

Recommended Posts

i have a script that lists the contents of the items in the dorectory where the index.php resides.  but when it lists the files it lists everything.  i want it to parse out(not show) 2 files types by extention.  no matter what i do i cant get it to work correctly.  can someone please help me out.  i have included the directory list script below in the hopes that someone might have a solution for me.

<?php

function direcho($path) {
global $filetotal, $fullsize, $totaldirs;
  $ignore = array(".php",".jpg");
    $ext = strtoupper(substr(strrchr($file_name, "."), 1));
if ($dir = opendir($path)) {
  while (false !== ($file = readdir($dir))) {
  if (is_dir($path."/".$file)) {                    // if it's a dir, check it's contents too
  if ($file != '.' && $file != '..' ) {            // but dont go recursive on '.' and '..'
    echo '<li><img src="folder.jpg"><a href =\ $file )><b>' . $file . '</b></a></li><ul>';
    direcho($path."/".$file );
    echo '</ul>';
    $totaldirs++;
  }
  }
  else {                      //if it's not a dir, just output.
  $tab = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  $filesize = $tab . '(' . filesize ($path.'/'.$file ) . ' bytes)';
  echo '<li><A HREF=' . $file . '>' . $file . '</A></li>';
  $fullsize = $fullsize + filesize ($path.'/'.$file);
  $filetotal++;
  }
}
closedir($dir);
}
}

direcho('.');

$fullsize = round($fullsize / 1024 / 1024, 2);

echo "<font size=\"2\" face=\"Arial, Helvetica, sans-serif\">
<b>Total files</b> - $filetotal files<br>
<b>Total dirs</b> - $totaldirs directories<br>
<b>Total size</b> - $fullsize MB<br>";
?>
any help would be greatly appreciated.
mike
Link to comment
https://forums.phpfreaks.com/topic/33096-directory-list-script-help-for-n00b/
Share on other sites

Try this:
[code=php:0]
<?php

function direcho($path) {
global $filetotal, $fullsize, $totaldirs;
   $ignore = array(".php",".jpg");
    $ext = strtoupper(substr(strrchr($file_name, "."), 1));
if ($dir = opendir($path)) {
  while (false !== ($file = readdir($dir))) {
   if (is_dir($path."/".$file)) {                    // if it's a dir, check it's contents too
   if ($file != '.' && $file != '..' ) {            // but dont go recursive on '.' and '..'
     echo '<li><img src="folder.jpg"><a href =\ $file )>' . $file . '[/url]</li><ul>';
     direcho($path."/".$file );
     echo '</ul>';
     $totaldirs++;
   }
   }
  elseif(!stripos($file, ".php") && !stripos($file, ".jpg")) {                      //if it's not a dir, just output.
   $tab = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
   $filesize = $tab . '(' . filesize ($path.'/'.$file ) . ' bytes)';
   echo '<li><A HREF=' . $file . '>' . $file . '[/url]</li>';
   $fullsize = $fullsize + filesize ($path.'/'.$file);
   $filetotal++;
  }
}
closedir($dir);
}
}

direcho('.');

$fullsize = round($fullsize / 1024 / 1024, 2);

echo "<font size=\"2\" face=\"Arial, Helvetica, sans-serif\">
Total files - $filetotal files

Total dirs - $totaldirs directories

Total size - $fullsize MB
";
?>[/code]

Looks like part of your problem is that the code you were trying wasn't in the while loop.

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.