Need to edit this opendir script

Discussion in 'PHP' started by amandeepmail, Aug 30, 2008.

  1. #1
    <php>
    
    
    
    <?
    echo ("<h1>Directory Overzicht:</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 == "..") 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";
    ?>
    
    
    </php>
    Code (markup):
    I want this code to display content of some other directory instead of home directory where the file is stored. Can you help me out.
     
    amandeepmail, Aug 30, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $files = getFiles("./");
    
    PHP:
    ... you have to specify the path to the directory in this line.
     
    nico_swd, Aug 30, 2008 IP
    amandeepmail likes this.
  3. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The script worked fine. I edited the extension by mistake which created errors. Thanks for telling.
     
    amandeepmail, Aug 30, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Show me the path you've used.
     
    nico_swd, Aug 30, 2008 IP
  5. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Also can I display only files with some specific extension. ie files with .exe or .mp3 .
     
    amandeepmail, Aug 30, 2008 IP