...

View Full Version : Php List Files In Dir



Troy297
03-15-2007, 04:18 AM
Hey,

Making a script... the usual yada yada... (sounds like yoda, odd... anyways).... :rolleyes:

I made an uploader form, and now I need a script that displays all the files in the upload directory in a simple table but am clueless as to how to do it....? I came across a script that does what I wanted but i couldn't get it to work... here's the snippet for the file list part:


$upload_dir = '../uploads/';
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a>";
if ($DELETABLE)
$filelist .= " <a href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a>";
$filelist .= "<sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}

So would anyone be able to give me a good script that would do what I want?

Thanks in advance! :)

warlord
03-15-2007, 05:00 AM
Hey,

Making a script... the usual yada yada... (sounds like yoda, odd... anyways).... :rolleyes:

I made an uploader form, and now I need a script that displays all the files in the upload directory in a simple table but am clueless as to how to do it....? I came across a script that does what I wanted but i couldn't get it to work... here's the snippet for the file list part:


$upload_dir = '../uploads/';
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir$file'>".$file."</a>";
if ($DELETABLE)
$filelist .= " <a href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a>";
$filelist .= "<sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub>";
$filelist .="<br>";
}
}

So would anyone be able to give me a good script that would do what I want?

Thanks in advance! :)

This will list all files in a directory and put them in a table ~ very simplified ... If you want to display only certain types of files you can put the extension in (*.*) like (*.mp3), etc ...


<?php

foreach (glob("*.*") as $filename) {
$files = array($filename => $filename);

$list .= "\n".'<tr><td>' . $filename .'</td></tr>'."\r\n";

}

echo '<table>';

echo ($list);

echo '</table>';

?>

Regards ~

Troy297
03-15-2007, 05:16 AM
Thanks! That works great.... but how do i specify a remote directory?

warlord
03-15-2007, 05:54 AM
Thanks! That works great.... but how do i specify a remote directory?

... like this ...



glob('/pathto/files/*.*');

Fou-Lu
03-15-2007, 06:29 AM
... like this ...



glob('/pathto/files/*.*');

>.<
Thats a relative directory. Glob does not work on remote directory.

Troy297
03-15-2007, 03:07 PM
Ya lol... the relative directory is what I meant... :confused: I had tried to edit the code previously except I kept trying the following instead of the right way!


glob('*/pathto/files/*');

Anyways got it figured out now. Thanks for all the help! +Rep for all!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum