Display directory content using PHP

1 replies
Hi. I got this script from the internet which serves my purpose of just displaying the content in my directory so that people can download it. I just have one query.

Code:
function printFiles($path,$level)
    {
        if (is_dir($path))
        {
            if ($dh = opendir($path))
            {
                $spaces = str_repeat( ' ',($level*10));//spaces indent
                while (($file = readdir($dh)) !== false)
                {
                    $Filename=$path.$file;
                    $pos=strpos($file, ".");
                    if ($pos!=0||$pos===false)
                    {//no hidden files
                            if (is_dir($Filename))
                            {//directory
                                    echo $spaces."<b>". $file."</b><br/>";
                                    printFiles($Filename."/",$level+1);//recursive!
                            }
                            else
                            {
                                echo "$spaces<a href='$file'>".$file."</a><br/>";//normal files
                            }
                    }
                }
                closedir($dh);
                echo "<br>";
            }
        }
    }
    printFiles($_SERVER["DOCUMENT_ROOT"]."/temp_folder/",0);//start!
The script uses only one "=" sign in the if statement. It doesnt work if there are two.

How is this possible? And also how can I modify this code to display the file size?
#content #directory #display #php
  • Profile picture of the author imarketstuff
    the (double equals) will never work because the variable (dh) will never equal the return value of the opendir function before the check.

    the conditional is just checking if the directory handle (dh) exists at the time u open the directory, or basically if the function returns a true.

    for the file size, read up on:

    PHP: filesize - Manual

    peace
    Signature
    I MARKET STUFF

    {{ DiscussionBoard.errors[1936416].message }}

Trending Topics