List files in folder?

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
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

List files in folder?

Post by Mini-Me »

Ok, I just got a fileserver started with mIRC... on top of THAT, I just added php support. I am quite happy with myself. ^_^

So anyway, how can I echo all the files in a folder? (with them set inside anchors). Also, I'd like to add an icon next to certain types. (piece of paper next to txt files, etc).

So what's a simple way to do this?
(*crosses his fingers on 'simple'*)
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

<?PHP
$whatsinthedir=opendir ('.');
while ($file = readdir ($whatsinthedir)) {
  echo "$file <br>";
}
closedir($whatsinthedir); 
?>
You look like a smart try by gving a click HERE[/url]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

aHAH, THAT's what I want! ^_^ Thanks.

hmm... but is there a way to have it do a web version of that? In other words, I just opened up my entire comp to the net. ^^; I'd rather limit it to the one webfolder and display at the top / for the home dir, rather than C:/blahblah/files/blah
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

well, I've played around with it, but for some reason it keeps wanting to load something or whatever sometimes.... like it kinda freezes. What did I do wrong in the parts I changed?:

Code: Select all

if (isset($_GET['action']))
{
    switch($_GET['action'])
    {
        case 'cd':
          if (substr_count($_GET['path'], "..") == '0' || $_SESSION['cwd'] != 'C:\BeZkript_me\webfiles\files') {
            @chdir($_GET['path']);
          }
          $_SESSION['cwd'] = getcwd();
          break;
        case 'get':
            header('Content-length: ' . filesize($_GET['path']));
            header('Content-type: application/octetstream');
            header('Content-Disposition: attachment; filename=' . $_GET['path']);
            readfile($_GET['path']);
            exit();
        default:
    }
}

Code: Select all

function output_all_files($path)
{
    echo "<div><table>";
    if ($path != 'C:\BeZkript_me\webfiles\files' && $path != '.') {
      echo "<tr><td width='30'><img src='img/back.png' /></td><td width='300'><a href='?action=cd&path=..'>Parent Directory</a></td><td width='100'>&nbsp;</td><td width='180'>&nbsp;</td></tr>";
      echo "<tr><td colspan='4'><hr width='100%' /></td></tr>";
    }
//...

Code: Select all

<div>Current Location:
<?php
 $currentpath = $_SESSION['cwd'];
 if ($currentpath == 'C:\BeZkript_me\webfiles\files' || $currentpath == '.') {
   echo '/';
 }
 else {
   $currentpath = str_replace("C:\BeZkript_me\webfiles\files", "", $currentpath);
   echo str_replace("", "/", $currentpath);
 }
?>
</div>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I assume you want to allow people only to a given part of the filesystem?
this sample limits them to /home/timvw/src/home.mysth.be/webroot

Code: Select all

// handle actions
if (isset($_GET['action']))
{
    switch($_GET['action'])
    {
          ... i cutted some code away
    }
}

// this was added 
if (!preg_match("/^\/home\/timvw\/src\/home.mysth.be\/webroot/", getcwd()))
{
    echo "<b>This demo allows you only to browse in /home/timvw/src/home.mysth.be/webroot</b>";
    @chdir('/home/timvw/src/home.mysth.be/webroot');
    $_SESSION['cwd'] = getcwd();
}
User avatar
genetix
Forum Contributor
Posts: 115
Joined: Fri Aug 01, 2003 7:40 pm
Location: Sask, Regina
Contact:

Post by genetix »

Mind if I use the File Type code you used? The array and the images. It would save me a lot of time.

I would be willing to put your name in the page credentials.
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

alrighty! thanks. Now how can I make it work for that folder AND all subfolders?

btw, got it to change the windows address to linux style w/ the main dir as home ^_^ :

Code: Select all

$location = str_replace('C:\\BeZkript_me\\webfiles', '', $_SESSION['cwd']);
  $location = str_replace('\\files', '/', $location);
  echo str_replace('\'', '/', $location);
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

genetix wrote:Mind if I use the File Type code you used? The array and the images. It would save me a lot of time.
you can do whatever you want to do with that code ;)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Mini-Me wrote:alrighty! thanks. Now how can I make it work for that folder AND all subfolders?
it already works for subfolders, because i only see if the start of the cwd (current working directory) starts with /home/timvw/whatever

if i only wanted to allow a folder i would use ^/whatever/path$ as regular expression.
Mini-Me
Forum Commoner
Posts: 38
Joined: Sun Sep 12, 2004 4:07 pm

Post by Mini-Me »

>.> oh. Well, I guess that means I'm doing something wrong. ^^; I probably just put the path in wrong... ^^;
Here's what I have:

Code: Select all

if (!preg_match("/C:\/BeZkript_me\/webfiles\/files/", getcwd()))
{
    @chdir('C:\\BeZkript_me\\webfiles\\files');
    $_SESSION['cwd'] = getcwd();
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

meaby you could try case insensitive preg_match?

btw, i've changed the code, made it more flexible... currently output_html function accepts an array with the files (and info on them).

this way i can first retrieve the files and then generate output. the nice thing is that i can build such an array from a ftp-listing etc... here is a little sample i use to browse a different/remote filesystem:

Code: Select all

<?php
error_reporting(E_ALL);

function xml_list_files($path)
{
    $xml = '<folder>';
    if (($files = glob("$path/*")) !== false)
    {
        foreach ($files as $file)
        {
            $name = htmlentities(basename($file));
            $ctime = htmlentities(date('Y-m-d H:i:s', filemtime($file)));
            $real = htmlentities(realpath($file));

            // handle every file, execpt . and ..
            if ($file != '.' && $file != '..')
            {
                if (is_link($file))
                {
                    $source = htmlentities(readlink($file));
                    $xml .= "<file type='link' ctime='$ctime' real='$real' src='$source'>$name</file>";
                }
                elseif (is_dir($file))
                {
                    $xml .= "<file type='directory' ctime='$ctime' real='$real'>$name</file>";
                }
                else
                {
                    $size = htmlentities(filesize($file));
                    $xml .= "<file type='file' ctime='$ctime' real='$real' size='$size'>$name</file>";
                }
            }
        }
    }
    $xml .= '</folder>';
    return $xml;
}

if (isset($_GET['path']))
{
    $path = $_GET['path'];
}
else
{
    $path = '.';
}
echo xml_list_files($path);
?>
Post Reply