Results 1 to 3 of 3

Thread: Finding latest file in a directory

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Finding latest file in a directory

    Hello there I'm not to familer with working with directorys in PHP as I dont normally work with them in this in way. However what I'm after is a some php that will find the latest file uploaded into a directory and also its size and echo the name of the file and its size out. ie

    Camera.jpg 26/06/07

    Anyone have a simple script that can accomplish this?

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Finding latest file in a directory

    this may help you (From http://www.php.net):

    PHP Code:
    <?php

            
    function permission($filename)
            {
                
    $perms fileperms($filename);

                if     ((
    $perms 0xC000) == 0xC000) { $info 's'; }
                elseif ((
    $perms 0xA000) == 0xA000) { $info 'l'; }
                elseif ((
    $perms 0x8000) == 0x8000) { $info '-'; }
                elseif ((
    $perms 0x6000) == 0x6000) { $info 'b'; }
                elseif ((
    $perms 0x4000) == 0x4000) { $info 'd'; }
                elseif ((
    $perms 0x2000) == 0x2000) { $info 'c'; }
                elseif ((
    $perms 0x1000) == 0x1000) { $info 'p'; }
                else                                 { 
    $info 'u'; }

                
    // владелец
                
    $info .= (($perms 0x0100) ? 'r' '-');
                
    $info .= (($perms 0x0080) ? 'w' '-');
                
    $info .= (($perms 0x0040) ? (($perms 0x0800) ? 's' 'x' ) : (($perms 0x0800) ? 'S' '-'));

                
    // группа
                
    $info .= (($perms 0x0020) ? 'r' '-');
                
    $info .= (($perms 0x0010) ? 'w' '-');
                
    $info .= (($perms 0x0008) ? (($perms 0x0400) ? 's' 'x' ) : (($perms 0x0400) ? 'S' '-'));

                
    // все
                
    $info .= (($perms 0x0004) ? 'r' '-');
                
    $info .= (($perms 0x0002) ? 'w' '-');
                
    $info .= (($perms 0x0001) ? (($perms 0x0200) ? 't' 'x' ) : (($perms 0x0200) ? 'T' '-'));

                return 
    $info;
            }

            function 
    dir_list($dir)
            {
                if (
    $dir[strlen($dir)-1] != '/'$dir .= '/';

                if (!
    is_dir($dir)) return array();

                
    $dir_handle  opendir($dir);
                
    $dir_objects = array();
                while (
    $object readdir($dir_handle))
                    if (!
    in_array($object, array('.','..')))
                    {
                        
    $filename    $dir $object;
                        
    $file_object = array(
                                                
    'name' => $object,
                                                
    'size' => filesize($filename),
                                                
    'perm' => permission($filename),
                                                
    'type' => filetype($filename),
                                                
    'time' => date("d F Y H:i:s"filemtime($filename))
                                            );
                        
    $dir_objects[] = $file_object;
                    }

                return 
    $dir_objects;
            }

    ?>

    // call:

    <?php

            print_r
    (dir_list('/path/to/you/dir/'));

    ?>
    output sample:

    Array
    (
    [0] => Array
    (
    [name] => api
    [size] => 0
    [perm] => drwxrwxrwx
    [type] => dir
    [time] => 28 May 2007 01:55:02
    )

    [1] => Array
    (
    [name] => classes
    [size] => 0
    [perm] => drwxrwxrwx
    [type] => dir
    [time] => 26 May 2007 00:56:44
    )

    [2] => Array
    (
    [name] => config.inc.php
    [size] => 143
    [perm] => -rw-rw-rw-
    [type] => file
    [time] => 26 May 2007 13:13:19
    )

    [3] => Array
    (
    [name] => index.php
    [size] => 131
    [perm] => -rw-rw-rw-
    [type] => file
    [time] => 26 May 2007 22:15:18
    )

    [4] => Array
    (
    [name] => modules
    [size] => 0
    [perm] => drwxrwxrwx
    [type] => dir
    [time] => 28 May 2007 00:47:40
    )

    [5] => Array
    (
    [name] => temp
    [size] => 0
    [perm] => drwxrwxrwx
    [type] => dir
    [time] => 28 May 2007 04:49:33
    )

    )
    My usual boring signature: Something

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Finding latest file in a directory

    If you want to sort the array by time you need to use the creation time as indexes. You can do this by modifying just one function in the function dclamp posted:
    Code:
    $dir_objects[filectime($filename)] = $file_object;
    You can now sort the contents of the array using ksort() for ascending and krsort() for descending. Obviously, the first / last element of the array depending in the function you use will be the latest file.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

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