Jump to content

listing files in directory?


marcus

Recommended Posts

What you could do is make it display files on that website and then make a iframe to the file that displays the files. Heres the code to display what is in a database:

[code]
<?
echo ("<h1></h1>");

function getFiles($path) {
  //Function takes a path, and returns a numerically indexed array of associative arrays containing file information,
  //sorted by the file name (case insensitive).  If two files are identical when compared without case, they will sort
  //relative to each other in the order presented by readdir()
  $files = array();
  $fileNames = array();
  $i = 0;
 
  if (is_dir($path)) {
      if ($dh = opendir($path)) {
          while (($file = readdir($dh)) !== false) {
              if ($file == "." || $file == ".." || $file == "index.php") continue;
              $fullpath = $path . "/" . $file;
              $fkey = strtolower($file);
              while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
              $a = stat($fullpath);
              $files[$fkey]['size'] = $a['size'];
              if ($a['size'] == 0) $files[$fkey]['sizetext'] = "-";
              else if ($a['size'] > 1024) $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
              else if ($a['size'] > 1024*1024) $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
              else $files[$fkey]['sizetext'] = $a['size'] . " bytes";
              $files[$fkey]['name'] = $file;
              $files[$fkey]['type'] = filetype($fullpath);
              $fileNames[$i++] = $fkey;
          }
          closedir($dh);
      } else die ("Cannot open directory:  $path");
  } else die ("Path is not a directory:  $path");
  sort($fileNames,SORT_STRING);
  $sortedFiles = array();
  $i = 0;
  foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f];
 
  return $sortedFiles;
}

$files = getFiles("./");
foreach ($files as $file) print "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"$file[name]\">$file[name]</a></b><br>\n";
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.