Results 1 to 3 of 3

Thread: [RESOLVED] PHP List Files in a Directory

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    Resolved [RESOLVED] PHP List Files in a Directory

    hi guys, I have this code which I got from the web.

    Why is it's not able to read the folders in c: drive?

    What I mean is, I'm using Wamp for Windows and if I run this file the default directory is c:\wamp

    How to change this code so it's able to read c:\ drive?

    //Create output
    $directory = getFolderTree('..\temp');
    $htmlTree = createTree($directory["temp"]);

    ..\temp is under c:\wamp\temp
    if I will just put \temp to read as c:\temp

    There will be an error.


    Thank you for any help.

    PHP Code:
    <?php
    /**
     * Recovers folder structure and files of a certain path
     * 
     * @param string $path Folder where files are located
     * @param string $pattern Filter by extension
     * @param string $flags Flags to be passed to the glob
     * @return array Folder structure
     */
    function getFolderTree($path)
    {
        
    //Recovers files and directories
        
    $paths glob($path  "*"GLOB_MARK GLOB_ONLYDIR GLOB_NOSORT);
        
    $files glob($path  .  "*");
        
    //Traverses the directories found
        
    foreach ($paths as $key => $path)
        {
            
    //Create directory if exists
            //$directory = explode("\\", $path);
            
    $directory explode("\\"$path);
            unset(
    $directory[count($directory) - 1]);
            
    $directories[end($directory)] = getFolderTree($path);
            
    //Verify if exists files
            
    foreach ($files as $file)
            {
                if (
    strpos(substr($file2), ".") !== false)
                    
    $directories[] = substr($file, (strrpos($file"\\") + 1));
            }
        }
        
    //Return the directories
        
    if (isset($directories))
        {
            return 
    $directories;
        }
        
    //Returns the last level of folder
        
    else
        {
            
    $files2return = Array();
            foreach (
    $files as $key => $file)
                
    $files2return[] = substr($file, (strrpos($file"\\") + 1));
            return 
    $files2return;
        }
    }
    /**
     * Creates the HTML for the tree
     * 
     * @param array $directory Array containing the folder structure
     * @return string HTML
     */
    function createTree($directory)
    {
        
    $html "<ul>";
        foreach(
    $directory as $keyDirectory => $eachDirectory)
        {
            if(
    is_array($eachDirectory))
            {
                
    $html .= "<li class='closed'><span class='folder'>" $keyDirectory "</span>";
                
    $html .= createTree($eachDirectory);
                
    $html .=  "</li>";
            }
            else
            {
                
    $html .= "<li><span class='file'>" $eachDirectory "</span></li>";
            }
        }
        
    $html .= "</ul>";
        return 
    $html;
    }
    //Create output
    $directory getFolderTree('..\temp');
    $htmlTree createTree($directory["temp"]);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
            <title>PHP Directories</title>
            <link rel="stylesheet" href="http://jquery.bassistance.de/treeview/jquery.treeview.css" />
            <link rel="stylesheet" href="http://jquery.bassistance.de/treeview/demo/screen.css" />
            <script src="http://jquery.bassistance.de/treeview/lib/jquery.js" type="text/javascript"></script>
            <script src="http://jquery.bassistance.de/treeview/lib/jquery.cookie.js" type="text/javascript"></script>
            <script src="http://jquery.bassistance.de/treeview/jquery.treeview.js" type="text/javascript"></script>
            <script type="text/javascript" src="http://jquery.bassistance.de/treeview/demo/demo.js"></script>
        </head>
        <body>
            <div id="main">
                <ul id="browser" class="filetree">
                    <?php echo $htmlTree;?>
                </ul>
            </div>
        </body>
    </html>
    The taller the bamboo grows the lower it bends...

  2. #2
    Lively Member
    Join Date
    Nov 2006
    Posts
    67

    Re: PHP List Files in a Directory

    I think this code does what you want. It's from an old hobby site, not intendet for public sites.
    It ran on a Linux-server but I don't think that should not be a problem.
    Anyway, give it at try.

    PHP Code:
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <link rel="stylesheet" href="../main.css" type="text/css">
    <style type="text/css">
    #bgc {
    background-color:#D8D8D8;
    }
    #bgc2 {
    background-color:#A4A4A4;
    }
    -->
    </style></head>
    <body>
    <?php
    $path 
    "./";
    if(isset(
    $_GET['path'])) { $path $_GET['path']; }
    $d scandir($path);
    $filer = array(); //Files
    $kataloger = array(); //Dirs
    echo 'Path: <b>',$path,'</b>';
    sort($filer);
    sort($kataloger);
    foreach(
    $d as $s)
    {
        if(
    is_file($s) == TRUE)
        {
            
    array_push($filer,$s);
        }
        if(
    is_dir($s) == TRUE)
        {
            
    array_push($kataloger,$s);
        }
    }
    $dir array_merge($kataloger$filer);
    ?> 
    <table cellspacing="0" bgcolor="">
    <?php
    foreach($dir as $x)
    {
    $color = ($i 2) ? "#dbfc0d" "#d7f903";
        if(
    is_dir($x) == TRUE
        {
            echo
    '<tr bgcolor=',$color,'><td style="color:#FF0000;">
            <a href="filer.php?path='
    ,$x,'" target="_self">',$x,'</a></td><td>
            </td></tr>'
    ;
        }
        else
        {
            echo
    '<tr bgcolor=',$color,'><td style="color:#321321;">
            '
    ,$x,'</td><td> ',filesize($x),' byte
            </td></tr>'
    ;
        }
    $i++;
    }    
    ?>    
    </table>
    </body>
    </html>
    FORZA ROSSONERI! CAMPIONI!!!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    Re: PHP List Files in a Directory

    Thanks, Rossonero for your reply

    changing this line:

    $directory = explode("\\", $path);

    to:

    $directory = explode(DIRECTORY_SEPARATOR, $path);

    These lines also:

    $directory = getFolderTree('..\temp');
    $htmlTree = createTree($directory["temp"]);

    change to:

    $pathx = '/temp/';

    $directory = getFolderTree($pathx );

    $htmlTree = createTree($directory);

    and it works!!
    The taller the bamboo grows the lower it bends...

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