www.webdeveloper.com
Results 1 to 5 of 5

Thread: Read Directory

  1. #1
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046

    Read Directory

    Hi here is my code to read the files in a directory, now I would like to only return the 25 most newly created files in ascending order. how can I do this?

    Some of my directories can have 100's of files, but I only need the 25 newest files to display in ascending order.

    Thanks for any help

    PHP Code:
    <html>
    <head>
    <title>FSL Directory Monitor</title>
    <script language="JavaScript" src="refresh.js" type="text/javascript"></script>
    </head>
    <body>
    <table align="center" width="95%" border="0">
    <tr>
    <td>
    <?php
    //------------------------------------------------------------//
    // Time Checked
    $when = `date`;
    //------------------------------------------------------------//

    // define the projects and their directories as CSV text:
    // Each new line should start with the project name, then task, then directory for that task, 
    // then next task and next directory. 
    // Best Viewing - AMOUNT of Tasks = 3 AMOUNT of Directories = 3
    // MAX AMOUNT of Tasks = 4, MAX AMOUNT of Directories = 4
    // EXAMPLE BELOW:
    // PROJECT_NAME,task_name1,directory_name1,TASK_NAME2,DIRECTORY_NAME2,task3_name,directory3_name,TASK_NAME4,DIRECTORY_NAME4

    $Dir_Info "PROJECT NAME,CHECK FOR ERROR,/example/error
    PROJECT2 NAME, CHECK LOCATION,/example/test1,CHECK HERE,/example/test2
    PROJECT3 NAME, AND HERE,/example/test3,LAST ONE,/example/test4 
    "
    ;

    $projectText = <<<EOD
    $Dir_Info
    EOD;
    //------------------------------------------------------------//
    // build $projects array from the CSV data:
    $projectLines preg_split("/[\r\n]+/"$projectText);
    $projects = array();
    $counter 0;
    foreach(
    $projectLines as $line)
    {
           if(
    trim($line) !== "")
           {
                  
    $parts explode(','$line);
                  
    $projects[$counter]['project'] = trim(array_shift($parts));
                  
    $projects[$counter]['task1'] = trim(array_shift($parts));
            
    $projects[$counter]['directory1'] = trim(array_shift($parts));
            
    $projects[$counter]['task2'] = trim(array_shift($parts));
                    
    $projects[$counter]['directory2'] = trim(array_shift($parts));
            
    $projects[$counter]['task3'] = trim(array_shift($parts));
                    
    $projects[$counter]['directory3'] = trim(array_shift($parts));
            
    $projects[$counter]['task4'] = trim(array_shift($parts));
                    
    $projects[$counter]['directory4'] = trim(array_shift($parts));
                  
    $counter++;
           }
    }
    //------------------------------------------------------------//
    // create listings for each project:
    foreach($projects as $proj)
    {
        global 
    $var_proj$var_task1$var_task2$var_task3$var_task4$var_dir1$var_dir2$var_dir3$var_dir4$when;

        
    //Set Global Variables
            
    $var_proj  $proj['project'];
            
    $var_task1 $proj['task1'];
            
    $var_task2 $proj['task2'];
            
    $var_task3 $proj['task3'];
            
    $var_task4 $proj['task4'];
            
    $var_dir1  $proj['directory1'];
            
    $var_dir2  $proj['directory2'];
            
    $var_dir3  $proj['directory3'];
            
    $var_dir4  $proj['directory4'];

        echo(
    "<TABLE cellspacing=0 cellpadding=2 id=header width=\"90%\">");
            echo(
    "<tr>");
            echo(
    "<td width=\"100%\" bgcolor=\"lightgrey\" nowrap >$var_proj &nbsp &nbsp - &nbsp &nbsp $when</td>");
            echo(
    "</tr>");
            echo(
    "</TABLE>");

            echo(
    "<div style=\"overflow: auto; width: 90%; height: 200; ");
            echo(
    "border-left: 1px gray solid; border-bottom: 1px gray solid; border-right: 1px gray solid;");
            echo(
    "padding:0px; margin: 0px\">");
            echo(
    "<table cellspacing=0 cellpadding=2>");
            echo(
    "<tr>");
            echo(
    "<td>&nbsp</td>");
            echo(
    "<td valign=\"top\" nowrap>");

        if (
    $var_task1 == "" && $var_dir1 == "")
        {
            echo(
    "<font color=\"red\">No Directory Configured</font>");
        }
        else
        {
            if (
    $var_task2 == "" && $var_dir2 == "")
            {
                
    check_4err();
            }
            else
            {
                if (
    $var_task3 == "" && $var_dir3 == "")
                        {
                    
    check_4err();
                        }
                        else
                        {
                    if (
    $var_task4 == "" && $var_dir4 == "")
                                {
                        
    check_4err();
                                }
                                else
                                {
                        
    check_4err();
                                }
                        }
            }
        }
       
        echo(
    "</td>");
            echo(
    "<td>&nbsp</td>");
            echo(
    "</tr>");
            echo(
    "</table>");
            echo(
    "</div>");
            echo(
    "<br>");
    }
    //------------------------------------------------------------//
    function check_4err()
    {
        global 
    $var_proj$var_task1$var_task2$var_task3$var_task4$var_dir1$var_dir2$var_dir3$var_dir4;

    // Directory 1

        // check for error directory
        
    if (ereg("error$"$var_dir1) && $var_dir1 != "")
        {
            
    $dir  $var_dir1;
                
    $proc $var_task1;
            
    $openDir=opendir($dir);
            
            
    make_err_dir($dir$proc$openDir);

        }
        else if (
    $var_dir1 == "")
        {
            echo (
    "Please check the CSV list for proper layout");
        }
          else
        {
            
    $dir  $var_dir1;
                
    $proc $var_task1;
            
    $openDir=opendir($dir);

            
    make_dir($dir$proc$openDir);

    //------------------------------------------------------------//
    // Directory 2

            
    if (ereg("error$"$var_dir2) && $var_dir2 != "")
            {
                
    $dir  $var_dir2;
                        
    $proc $var_task2;
                
    $openDir=opendir($dir);
                
                
    make_err_dir($dir$proc$openDir);
                   }
            else if (
    $var_dir2 == "")
            {
                
    $nothing true;
            }
            else
            {
                
    $dir  $var_dir2;
                        
    $proc $var_task2;
                
    $openDir=opendir($dir);

                
    make_dir($dir$proc$openDir);

    //------------------------------------------------------------//
    // Directory 3

                
    if (ereg("error$"$var_dir3) && $var_dir3 != "")
                {
                    
    $dir  $var_dir3;
                            
    $proc $var_task3;
                    
    $openDir=opendir($dir);

                    
    make_err_dir($dir$proc$openDir);
                }
                else if (
    $var_dir3 == "")
                {
                    
    $nothing true;
                }
                else
                {
                    
    $dir  $var_dir3;
                            
    $proc $var_task3;
                    
    $openDir=opendir($dir);

                    
    make_dir($dir$proc$openDir);
        

    //------------------------------------------------------------//
    // Directory 4

                    
    if (ereg("error$"$var_dir4) && $var_dir4 != "")
                    {
                        
    $dir  $var_dir4;
                                
    $proc $var_task4;
                        
    $openDir=opendir($dir);

                        
    make_err_dir($dir$proc$openDir);
        
                    }
                    else if (
    $var_dir4 == "")
                    {
                        
    $nothing true;
                    }
                    else
                    {
                        
    $dir  $var_dir4;
                                
    $proc $var_task4;
                        
    $openDir=opendir($dir);

                        
    make_dir($dir$proc$openDir);

                    }
                }
            }
        }
    }
    //------------------------------------------------------------//
    function make_err_dir($dir$proc$openDir)
    {
        echo(
    "<td valign=\"top\" nowrap>");
            echo(
    "<fieldset><legend>$proc</legend>");
            echo(
    "Location:<br><font color=\"red\">$dir</font><br><hr>");
            while(
    false !=($filereaddir($openDir)))
            {
            
    //$elapsed = (date ("H:i:s.", filemtime($dir."/".$file))) - ($when);
                    
    if(!preg_match('/^\./',$file))
                    {
                
    //echo "<font color=\"red\">$file </font>: " . date ("F d Y H:i:s.", filemtime($dir."/".$file))."<font color=\"red\">$elapsed</font>".'<br>';
                
    echo "<font color=\"red\">$file </font>: " date ("F d Y H:i:s"filemtime($dir."/".$file)).'<br>';
                    }
            }
            echo(
    "</fieldset><br>");
            echo(
    "</td>");
    }
    //------------------------------------------------------------//
    function make_dir($dir$proc$openDir)
    {
        echo(
    "<td valign=\"top\" nowrap>");
            echo(
    "<fieldset><legend>$proc</legend>");
            echo(
    "Location:<br><font color=\"green\">$dir</font><br><hr>");
            while(
    false !=($filereaddir($openDir)))
            {
                    if(!
    preg_match('/^\./',$file))
                    {
                echo 
    "<font color=\"blue\">$file : </font>" date ("F d Y H:i:s"filemtime($dir."/".$file)).'<br>';
                    }
            }
            echo(
    "</fieldset><br>");
            echo(
    "</td>");
            echo(
    "<td>&nbsp</td>");
    }
    //------------------------------------------------------------//

    ?>

    </td>
    </tr>
    </table>
    </body>
    </html>

  2. #2
    Join Date
    Oct 2006
    Posts
    52
    Hi Phill

    Here's one way that springs to mind.

    Rather than directly outputting the $file and modified date, you could store the data in an array using the filemtime($file) as the key, and $file as the value. You could then sort the array using key based sorting (ksort : http://uk.php.net/manual/en/function.ksort.php or more likely krsort (reverse key sort) http://uk.php.net/manual/en/function.krsort.php ). Then just output the first 25 items in the array.

    Hope this helps.

  3. #3
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046
    Thanks for the advice, but its not going as planned

    I changed the function to this, but I can not get the order to sort asc or desc

    PHP Code:
    function make_dir($dir$proc$openDir)
    {
            
    $dir_cnt 0;
            echo(
    "<td valign=\"top\" nowrap>");
            echo(
    "<fieldset><legend>$proc</legend>");
            echo(
    "Location:<br><font color=\"green\">$dir</font><br><hr>");
            while(
    false !=($filereaddir($openDir)))
            {
                    
    //Initialize the array
                    
    $dir_arr = array();

                    if(!
    preg_match('/^\./',$file))
                    {
                            
    //echo "<font color=\"blue\">$file : </font>" . date ("F d Y H:i:s", filemtime($dir."/".$file)).'<br>';
                            
    $file_time date ("mdYHis"filemtime($dir."/".$file));
                            
    $dir_arr[$file_time]="$file";
                    }

                    
    krsort($dir_arr);
                    foreach (
    $dir_arr as $key => $val)
                    {
                            echo(
    "<font color=\"blue\">$val</font> = DATE: $key <br>");
                            
    $dir_cnt++;
                    }

            }
            echo(
    "File Count: $dir_cnt <br>");
            echo(
    "</fieldset><br>");
            echo(
    "</td>");
            echo(
    "<td>&nbsp</td>");

    Any other help would be great

  4. #4
    Join Date
    Oct 2006
    Posts
    52
    It may be because you have mdYHis for the date format.

    Change this so that it is year->month->day (YmdHis) and see how it goes.

  5. #5
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    PHP Code:
    <?php

    $directory 
    '.';

    $handle opendir($directory) or die('I could not open '.$directory);

    while(
    false !== ($file readdir($handle)))
    {
        if(!
    preg_match('/^\./',$file))
        {
            
    $files[] = $file;
            
    $age[] = filemtime($directory.'/'.$file);
        }
    }

    closedir($handle);

    array_multisort($ageSORT_NUMERICSORT_DESC$files); 

    foreach(
    array_slice($files025) as $k => $v)
    {
        echo 
    '<p><b>'.$v.'</b> '.date('Y/m/d H:i:s'$age[$k]).'</p>';
    }

    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles