Need simple php or programming script to..(read inside)

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

  1. #1
    I need a simple php script or other script with which I can automatically add links to all files in a directory on my server. If you don't get what I mean here are details:
    1. I have a directory on my server as (website.com/abc/dir1/) that is Dir1 which has (take 50 files can change value).
    2. I have a wordpress blog which allows me to use php or other scripting inside the post.
    3. I need to add links to all the 50 files with filename as the anchor text of link.

    I need this type of script because I have sometimes 50-60 music files in a directory. I want to link to all of them for my users to download and It will use a lot of my time if I do link 1st then 2nd then 3rd and on on....
    I want this script to save my time so it automatically check the folder for files and if it find the files with given extension it should add links to all those files in the output html.

    I hope you guys will help me out!!!:)
    If you need more details please leave a reply...
     
    amandeepmail, Aug 30, 2008 IP
  2. amandeepmail

    amandeepmail Banned

    Messages:
    408
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Figured it myself. Also thanks to 'nico_swd' for help.

    <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):
     
    amandeepmail, Aug 30, 2008 IP