Order file by last mod

Discussion in 'PHP' started by punkrazio, Jul 24, 2008.

  1. #1
    Hi all
    i've got a directory where php creates a txt file every time someone update his stock.

    i create file txt like this:

    customer-last-update-stock-05-06-2008---10-29-42.txt

    i read the files in the directory with this script:

    
    if ($handle = opendir('/web/htdocs/directory/home/customer/update_stock/')) {
        while (false !== ($file = readdir($handle))) { 
            if ($file != "." && $file != "..") { 
                echo "<a href=\"../customer/update_stock/$file\" target=\"_blank\">$file</a><br>\n"; 
            } 
        }
        closedir($handle); 
    }
    
    PHP:
    now my question is..

    how can i order and print the files by last mod time??
    there is any way?

    hope you can help

    bye bye:cool:
     
    punkrazio, Jul 24, 2008 IP
  2. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Try reading about
    fopen()
    PHP:
    .

    By the way, Why don't you use MySQL database. It's much better on storing changes than a TXT file.
     
    PET, Jul 24, 2008 IP
  3. wdillsmith

    wdillsmith Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I agree, mysql would be better. But, if you want to do with files and you are on unix, you can also execute a shell command for 'ls -r' or 'ls -rt' depending on which order you want.
     
    wdillsmith, Jul 24, 2008 IP
  4. tgkprog

    tgkprog Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    http://forums.digitalpoint.com/showthread.php?t=925117

    <?php
    
    $show = 0; // Leave as 0 for all
    $dir = ''; // Leave as blank for current
    
    if($dir) chdir($dir);
    $files = glob( '*.{html,php,php4,txt}', GLOB_BRACE );
    usort( $files, 'filemtime_compare' );
    
    function filemtime_compare( $a, $b )
    {
        return filemtime( $b ) - filemtime( $a );
    }
    $i = 0;
    foreach ( $files as $file )
    {
        ++$i;
        if ( $i == $show ) break;
        echo $file . ' - ' . date( 'D, d M y H:i:s', filemtime($file) ) . '<br />' . "\n";  /* This is the output line */
    } 
    PHP:
     
    tgkprog, Jul 24, 2008 IP
  5. punkrazio

    punkrazio Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thank you all
    i have found this solution

    $arrayfile = Array();
    function ordina($file1,$file2)
    {
        $tempo1 = filectime($file1);
        $tempo2 = filectime($file2);
        return ($tempo1 < $tempo2) ? 1 : -1;
    }
    if ($handle = opendir('/web/htdocs/domain/home/customer/update_stock/'))
    {
        while (false !== ($file = readdir($handle)))
        {
            if ($file != "." && $file != "..") {
                $arrayfile[]='../customer/update_stock/'.$file;
            }
        }
        closedir($handle);
        usort($arrayfile,'ordina');
        //print_r($arrayfile);
        foreach($arrayfile as $txt)
        {
            echo "<a href=\"$txt\" target=\"_blank\">".str_replace("../customer/update_stock/","",$txt)."</a><br>\n";
        }
    }
    clearstatcache();
    PHP:
     
    punkrazio, Jul 25, 2008 IP
  6. sastro

    sastro Well-Known Member

    Messages:
    214
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #6
    This is mine

     
    sastro, Jul 25, 2008 IP