Results 1 to 33 of 33

Thread: PHP Dir list

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    PHP Dir list

    PHP Code:
    <?
    $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?

  2. #2
    denniswrenn
    Guest
    try this:

    $dir_name="./";

  3. #3
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    I don't think you would get the wanted effect for the $dir_name printout then....

    But other than that, it works

  4. #4

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Yeah, well, the bad comes with the good but oh well. Thanks

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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:
    PHP Code:
    <? 
    $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>

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Just the directory's name:
    PHP Code:

    <? 
    $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>

  7. #7
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    how would you get the full path of the files?

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    I think you mean showing

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

    Here you go:

    PHP Code:
    <? 
    $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>

  9. #9
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    thanks, but, i get an error with it.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  10. #10

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.
    Alcohol & calculus don't mix.
    Never drink & derive.

  11. #11
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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?

  12. #12
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Yeah, sorry sail, I didn't test the code that i posted before, try this:

    PHP Code:
    <? 
    $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>

  13. #13
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    cool, works now, thanks.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  14. #14

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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)
    Alcohol & calculus don't mix.
    Never drink & derive.

  15. #15
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Have you tried this code yet:

    I posted it before.

    PHP Code:
    <? 
    $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...

  16. #16

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Thanks, it works. One thing though, you forgot to escape the backslash, that threw me for a loop till i figured it out
    Alcohol & calculus don't mix.
    Never drink & derive.

  17. #17
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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

  18. #18
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    is there anyway to tell it how to sort the files?

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

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  19. #19
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    can't test it, but you should be able to load it into an array then sort the array...

  20. #20
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    how would i load it into an array?

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  21. #21
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    any ideas please?

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  22. #22
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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 )

  23. #23
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Alright, i'll remember.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  24. #24
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    good for you

    i think it would be quite difficult

    can't remember if php can handle multiple dimension arrays

  25. #25
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    that would be bad if it didn't. it would certainly make things difficult

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  26. #26
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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

  27. #27
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    sounds good. i still have no idea how to do it though.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  28. #28
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    here you go.

    hope that works.

    PHP Code:
    <?
    $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>";
    }
    ?>

  29. #29

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    The only part that works is the file size part. And, the filename and date parts print in the same order.
    Last edited by Wynd; Nov 27th, 2001 at 06:43 PM.
    Alcohol & calculus don't mix.
    Never drink & derive.

  30. #30

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Nm, got it to work. I have been looking for this for a long time. Thanks for the help!
    Alcohol & calculus don't mix.
    Never drink & derive.

  31. #31
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    No probs, what was the problem?

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

  32. #32

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    I just c&p'ed ity from the web page. In this part:
    PHP Code:
    each($file_list[1]) 
    you need to change "1" to 0, 1, or 2 depending on what you want.
    Alcohol & calculus don't mix.
    Never drink & derive.

  33. #33
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    oh yeah, woops i should've mentioned that in my other post :|

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width