PDA

Click to See Complete Forum and Search --> : PHP Dir list


Wynd
Nov 1st, 2001, 07:40 PM
<?
$dir_name = "C:\site";
$dir = opendir($dir_name);
$file_list = "<ul>";

while ($file_name = readdir($dir))
{
if (($file_name != ".") && ($file_name != ".."))
{
$file_list .= "<li>$file_name</li>";
}
}

$file_list .= "</ul>";

closedir($dir);
?>

<html>
<head>
<title>Files in <? echo "$dir_name"; ?></title>
Files in: <? echo "$dir_name"; ?>
<? echo "$file_list"; ?>
</body>
</html>Is there a way I can just put this in a directory and have it list the files for that dir, and not have to manually specify the dir to list the files of?

denniswrenn
Nov 1st, 2001, 08:59 PM
try this:

$dir_name="./";

da_silvy
Nov 2nd, 2001, 01:03 AM
I don't think you would get the wanted effect for the $dir_name printout then....

But other than that, it works ;)

Wynd
Nov 2nd, 2001, 08:03 PM
Yeah, well, the bad comes with the good but oh well. Thanks :)

da_silvy
Nov 2nd, 2001, 08:48 PM
What about having good with good:

This should be what you are looking for:

Did you want the absolute path or just directory name?

Absolute Path:

<?
$dir_name = "./";
$dir = opendir($dir_name);
$file_list = "<ul>";

while ($file_name = readdir($dir))
{
if (($file_name != ".") && ($file_name != ".."))
{
$file_list .= "<li>$file_name</li>";
}
}

$file_list .= "</ul>";

closedir($dir);
?>

<html>
<head>
<title>Files in <? echo getcwd(); ?></title>
Files in: <? echo getcwd(); ?>
<? echo "$file_list"; ?>
</body>
</html>

da_silvy
Nov 2nd, 2001, 08:53 PM
Just the directory's name:


<?
$dir_name = "./";
$dir = opendir($dir_name);
$file_list = "<ul>";

while ($file_name = readdir($dir))
{
if (($file_name != ".") && ($file_name != ".."))
{
$file_list .= "<li>$file_name</li>";
}
}

$file_list .= "</ul>";

closedir($dir);
$dir = substr(strrchr(getcwd(), "\\"), 1);
?>

<html>
<head>
<title>Files in <? echo $dir; ?></title>
Files in: <? echo $dir; ?>
<? echo "$file_list";?>
</body>
</html>

sail3005
Nov 4th, 2001, 09:28 AM
how would you get the full path of the files?

da_silvy
Nov 4th, 2001, 05:43 PM
I think you mean showing

Bullet: c:\apache\htdocs\index.php?

Here you go:


<?
$dir_name = "./";
$dir = opendir($dir_name);
$file_list = "<ul>";

while ($file_name = readdir($dir))
{
if (($file_name != ".") && ($file_name != ".."))
{
$filename = getcwd()."\". $file_name;
$file_list .= "<li>getcwd()\$file_name</li>";
}
}

$file_list .= "</ul>";

closedir($dir);
?>

<html>
<head>
<title>Files in <? echo getcwd(); ?></title>
Files in: <? echo getcwd(); ?>
<? echo "$file_list"; ?>
</body>
</html>

sail3005
Nov 4th, 2001, 06:08 PM
thanks, but, i get an error with it. :confused:

Wynd
Nov 4th, 2001, 07:16 PM
No, what I meant was the relative path i guess. Like if I put the file in C:\site\folder\dir\images\ it should say "Files in images/", if I put it in C:\site\db\ it should say "Files in db/", etc.

da_silvy
Nov 4th, 2001, 07:26 PM
Originally posted by Wynd
No, what I meant was the relative path i guess. Like if I put the file in C:\site\folder\dir\images\ it should say "Files in images/", if I put it in C:\site\db\ it should say "Files in db/", etc.

Have a look at my second post with code in it.

That does what you want ^^^^ here. ;)

Sail, what does the error say?

da_silvy
Nov 4th, 2001, 07:31 PM
Yeah, sorry sail, I didn't test the code that i posted before, try this:


<?
$dir_name = "./";
$dir = opendir($dir_name);
$file_list = "<ul>";

while ($file_name = readdir($dir))
{
if (($file_name != ".") && ($file_name != ".."))
{
$cwd = getcwd();
$file_list .= "<li>$cwd\\$file_name</li>";
}
}

$file_list .= "</ul>";

closedir($dir);
?>

<html>
<head>
<title>Files in <? echo getcwd(); ?></title>
Files in: <? echo getcwd(); ?>
<? echo "$file_list"; ?>
</body>
</html>

sail3005
Nov 5th, 2001, 03:38 PM
cool, works now, thanks. :D

Wynd
Nov 5th, 2001, 06:27 PM
Well that's partly what I want. I just want the script to output the directory name, not the whole path. (C:\path\dir\images produces "Files in images", C:\otherpath\scripts produces "Files in scripts", etc)

da_silvy
Nov 5th, 2001, 11:04 PM
Have you tried this code yet:

I posted it before. :rolleyes:

<?
$dir_name = "./";
$dir = opendir($dir_name);
$file_list = "<ul>";

while ($file_name = readdir($dir))
{
if (($file_name != ".") && ($file_name != ".."))
{
$file_list .= "<li>$file_name</li>";
}
}

$file_list .= "</ul>";

closedir($dir);
$dir = substr(strrchr(getcwd(), "\"), 1);
?>

<html>
<head>
<title>Files in <? echo $dir; ?></title>
Files in: <? echo $dir; ?>
<? echo "$file_list";?>
</body>
</html>



It does what you want...

Wynd
Nov 6th, 2001, 05:59 PM
Thanks, it works. One thing though, you forgot to escape the backslash, that threw me for a loop till i figured it out :o

da_silvy
Nov 7th, 2001, 01:30 AM
Woops, I didn't actually, but the php tags did that.

If you quote my message, you will see that i didn't forget to escape it ;)

sail3005
Nov 22nd, 2001, 12:28 AM
is there anyway to tell it how to sort the files?

Like by, date modified, size, name, etc?

da_silvy
Nov 22nd, 2001, 12:53 AM
can't test it, but you should be able to load it into an array then sort the array...

sail3005
Nov 22nd, 2001, 08:29 AM
how would i load it into an array?

sail3005
Nov 23rd, 2001, 01:40 PM
any ideas please?

da_silvy
Nov 23rd, 2001, 06:16 PM
I think it's pretty difficult

I will have heaps of free time in a week, i'll figure something out, so just PM me. ;) after thursday - (no more exams ;))

sail3005
Nov 23rd, 2001, 08:36 PM
Alright, i'll remember. ;)

da_silvy
Nov 23rd, 2001, 09:09 PM
good for you

i think it would be quite difficult

can't remember if php can handle multiple dimension arrays :eek:

sail3005
Nov 23rd, 2001, 10:37 PM
that would be bad if it didn't. it would certainly make things difficult

da_silvy
Nov 24th, 2001, 12:25 AM
i was thinking though

It could be put into a normal array, depending on what order they want

i.e.

By FIlename

then the string =

"filename, size, " etc


By size

then the string =

"size, filename, " etc


then you sort the array
and split it

sail3005
Nov 24th, 2001, 10:49 AM
sounds good. i still have no idea how to do it though. :(

da_silvy
Nov 27th, 2001, 03:27 AM
here you go.

hope that works.


<?
$dir_name = "./";
$dir = opendir($dir_name);
$myint = -1;
while ($file_name = readdir($dir))
{
if (($file_name != ".") && ($file_name != "..") && !is_dir($file_name))
{
$myint++;
$file_list[0][$myint] = "$file_name";
$file_list[1][$myint] = filesize($file_name);
$file_list[2][$myint] = filemtime($file_name);
}
}
//asort($file_list[0]); Sort by filename
//asort($file_list[1]); Sort by filesize
//asort($file_list[2]); Sort by modified time
asort($file_list[1]);
while (list($key) = each($file_list[1])) {
print
$file_list[0][$key].",".$file_list[1][$key].",".$file_list[2][$key]."<br>";
}
?>

Wynd
Nov 27th, 2001, 05:38 PM
The only part that works is the file size part. And, the filename and date parts print in the same order.

Wynd
Nov 27th, 2001, 05:47 PM
Nm, got it to work. I have been looking for this for a long time. Thanks for the help!

da_silvy
Nov 28th, 2001, 12:53 AM
No probs, what was the problem?

Did you quote the message and then copy that source or what?

Wynd
Nov 28th, 2001, 05:23 PM
I just c&p'ed ity from the web page. In this part:each($file_list[1])you need to change "1" to 0, 1, or 2 depending on what you want.

da_silvy
Nov 29th, 2001, 02:01 AM
oh yeah, woops i should've mentioned that in my other post :|