Display folder contents as clickable links?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
deBassMan
Forum Newbie
Posts: 4
Joined: Sun Aug 19, 2007 2:58 am

Display folder contents as clickable links?

Post by deBassMan »

Hi All

I need to display the contents of a folder (i.e all files that match a four character prefix).

The files may include .pdf, .doc .jpg etc.

I'd like to display the filenames as clickable links which, when clicked would open and display the file (using associated applications)

Would anyone have an example of a script that might enable this?

many thanks for your time and expertise ...

scott
User avatar
pedrokas
Forum Commoner
Posts: 32
Joined: Thu Jan 15, 2004 10:53 am
Location: Lisboa, Portugal

Re: Display folder contents as clickable links?

Post by pedrokas »

this is the way i made for a folder of pdf's!

Code: Select all

 
function comparar($a, $b)
{
    return strnatcasecmp($b["1"],$a["1"]);
}
$default_dir = ".";
$dir_lista = array();
$dir = dir($default_dir);
while($file = $dir->read())
 {
    if(0 != strpos($file, '.pdf'))
    {
        $ultmod=filemtime($file);
        $dir_lista[]= array($file,$ultmod);
    }
 }
$dir->close();
usort($dir_lista, "comparar");
$arraynum= sizeof($dir_lista);
$i =0;
while($i<$arraynum)
 {
    $corLista=$dir_lista[$i];
    $corFich = $corLista[0];
    $corTempo = $corLista[1];
    $corDate= date("Y-m-d",$corTempo);
    $nome= substr($corFich,0,-4); 
 
    echo "<tr>";
    echo "<td align=\"center\" valign=\"middle\"><div align=\"left\" class=\"col\">( <span class=\"style4\">$curDate </span>)&nbsp;&nbsp; <a href='$curFich'><strong>$nome</strong>  </a></div></td>";
    echo "</tr>";
    $i = $i + 1;
 }
  
hope it helped!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Display folder contents as clickable links?

Post by pickle »

Code: Select all

<?PHP
 
/* The full, absolute path to the directory */
$file_system_dirname = '/path/to/directory';
 
/* The part of the path that's below the web root */
$web_dirname = '/to/directory';
 
 
chdir($file_system_dirname);
$all_files = glob('*');
natsort($all_files);
foreach($all_files as $filename)
{
   echo <<<LINK
<a href = "$web_dirname/$filename">$filename</a>
LINK;
}
 
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
deBassMan
Forum Newbie
Posts: 4
Joined: Sun Aug 19, 2007 2:58 am

Re: Display folder contents as clickable links?

Post by deBassMan »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi there
and thanks for the replies ...

here's how i did it ....

Code: Select all

foreach (glob("*.*") as $filename)
      echo "<a href=\"".$filename."\">$filename</a><br/>";
}
 

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Post Reply