Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,493 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,684 people online right now. Registration is fast and FREE... Join Now!




Simple File Browser help

 
Reply to this topicStart new topic

Simple File Browser help, PHP

javster87
6 Mar, 2008 - 05:26 PM
Post #1

New D.I.C Head
*

Joined: 6 Mar, 2008
Posts: 3

Hi. I'm trying to write a simple PHP file browser that will parse a folder and display all the files and folders contained within it. The folders have to be clickable and open the folder and repeat the process. Also, i have to process the ".." directory so that the actual path of the parent folder is displayed as opposed to "/../". I need help with this last requirement. Here is my attempt to do that, which does not work. How can I fix this to display the path properly?


CODE

<HTML>
    <HEAD><TITLE> PHP File Browser </TITLE></HEAD>
    <BODY>
        <?PHP
            if(isset($_GET["path"]))
            {
                $path= $_GET["path"];
            }
            else
                $path="C:\Documents and Settings\HP_Administrator\My Documents";
            
            if(!($check = chdir($path)))
            {
                echo "Error! Path Not Found!";
            }
            else
            {
                $folder = opendir($path);
                $free = diskfreespace($path);
                
                echo "Let's check $path.<BR>\n";
                echo "It has $free bytes free.<BR>\n";
                
                $arr = scandir($path);
                $directories;
                $files;
                $a = 0;
                $b = 0;
                
                
                foreach($arr as $e)
                {
                    if(is_dir($e))
                    {
                        $directories[$a] = $e;
                        $a++;
                    }
                    else if(is_file($e))
                    {
                        $files[$b] = $e;
                        $b++;
                    }
                }
                
                echo "<BR>";
                
                natcasesort($directories);
                natcasesort($files);
                
                foreach($directories as $el)
                {
                     if($el != ".")
                     {
                      /*    if($el != "..")
                          {*/
                            echo "<A HREF="."\"http://localhost/file_browser.php?path=$path/".$el."\">"."$el"."</A>";
                            echo "<BR>\n";    
                        /*}
                        else
                        {
                            $n = strrpos($path, "\\");
                            $path2 = substr($path, 0, n);
                            echo "<A HREF="."\"http://localhost/file_browser.php?path=$path2\">"."$el"."</A>";
                            echo "<BR>\n";
                        }*/    
                    }
                    
                }
                
                echo "<BR>\n";
                
                foreach($files as $elem)
                {
                    echo $elem;
                    echo "<BR>\n";
                }
                    
            }
        function superExplode($str, $sep)
        {
            $i = 0;
            $arr[$i++] = strtok($str, $sep);
            while ($token = strtok($sep))
                $arr[$i++] = $token;
            return $arr;
        }
        ?>
    </BODY>
</HTML>

User is offlineProfile CardPM
+Quote Post

ahmad_511
RE: Simple File Browser Help
7 Mar, 2008 - 08:08 AM
Post #2

D.I.C Regular
Group Icon

Joined: 28 Apr, 2007
Posts: 351



Thanked: 8 times
Dream Kudos: 400
My Contributions
Hello
as I saw the problem is in the foreach loop, and it happened because some times it didn't suplied with array.
So do check if $files is an array or not
there are two places to edit
Here
php

if(is_array($files))natcasesort($files);

and here
php

if(is_array($files)){
foreach($files as $elem)
{
echo $elem;
echo "<BR>\n";
}
}


I hope it helps
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:53PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month