Jump to content

Recommended Posts

$path = "/home/themungo/public_html/up/";
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<br/>";
while ($file = readdir($dir_handle)) {
if($file!="." && $file!="..")
echo "<a href='$file'>$file</a><br/>";
}
closedir($dir_handle);

 

So heres a simple piece of code that lists the contents of a directory... Now is there any way I could have just the newest 20 files listed, not the whole directory?

Link to comment
https://forums.phpfreaks.com/topic/103179-file-directory-listing/
Share on other sites

Might try this ...

<?PHP 


######################################### 
# 
# list_dir_files.php 
# 
#    This is a compilation and modification of  
#    several scripts, snippets, tutorials and 
#    examples from the manual 
# 
#    Major credit goes to phpDIRList 2.0 - (c)2005 Ulrich S. Kapp :: Systemberatung :: web-programmierung 
# 
#    The script reads through a given folder 
#    and displays the files contained therein 
# 
#    There are five variables you may alter 
# 
#    1.    $allowed_ext - this array is used to set which 
#        type of file will be listed 
# 
#    2.    $directory - this is used to set which directory 
#        will be searched/listed 
# 
#    3.    $do_link - TRUE means display the file 
#        name as a link to the file; FALSE means 
#        do NOT make the name a link 
# 
#    4.    $sort_what - this determines what the 
#        data will be sorted upon: 0 is by name; 
#        1 is by size; 2 is by date last modified 
# 
#    5.    $sort_how - describes how to sort;  
#        0 means to sort ASCENDING  
#        (oldest to newest, smallest to largest, Z to A);  
#        while 1 means to sort DESCENDING 
#        (newest to oldest, largest to smallest, A to Z) 


##################################################### 
#    These are the five variables 
##################################################### 

$allowed_ext = array(".gif", ".png", ".jpg", ".jpeg", ".txt", ".doc", ".xls"); 
$directory = dir("./"); 
$do_link = TRUE; 
$sort_what = 0; 
$sort_how = 0; 

################################################# 
#    These are functions to perform the work 
################################################# 

function dir_list($dir){ 
    $i=0; 
    $dl = array(); 
    if ($hd = opendir($dir))    { 
        while ($sz = readdir($hd)) {  
            if (preg_match("/^\./",$sz)==0) $dl[] = $sz;$i.=1;  
        } 
    closedir($hd); 
    } 
    asort($dl); 
    return $dl; 
} 
if ($sort_how == 0) { 
    function compare0($x, $y) {  
        if ( $x[0] == $y[0] ) return 0;  
        else if ( $x[0] < $y[0] ) return -1;  
        else return 1;  
    }  
    function compare1($x, $y) {  
        if ( $x[1] == $y[1] ) return 0;  
        else if ( $x[1] < $y[1] ) return -1;  
        else return 1;  
    }  
    function compare2($x, $y) {  
        if ( $x[2] == $y[2] ) return 0;  
        else if ( $x[2] < $y[2] ) return -1;  
        else return 1;  
    }  
}else{ 
    function compare0($x, $y) {  
        if ( $x[0] == $y[0] ) return 0;  
        else if ( $x[0] < $y[0] ) return 1;  
        else return -1;  
    }  
    function compare1($x, $y) {  
        if ( $x[1] == $y[1] ) return 0;  
        else if ( $x[1] < $y[1] ) return 1;  
        else return -1;  
    }  
    function compare2($x, $y) {  
        if ( $x[2] == $y[2] ) return 0;  
        else if ( $x[2] < $y[2] ) return 1;  
        else return -1;  
    }  

} 

################################################## 
#    We get the information here 
################################################## 

$i = 0; 
while($file=$directory->read()) { 
    $file = strtolower($file); 
    $ext = strrchr($file, '.');  
    if(!in_array($ext,$allowed_ext)){ 
        // dump 
    }else { 
        $temp_info = stat($file); 
        $new_array[$i][0] = $file; 
        $new_array[$i][1] = $temp_info[7]; 
        $new_array[$i][2] = $temp_info[9]; 
        $new_array[$i][3] = date("F d, Y", $new_array[$i][2]); 
        $i = $i + 1; 
    } 
} 
$directory->close(); 

################################################## 
# We sort the information here 
################################################# 

switch ($sort_what) { 
    case 0: 
            usort($new_array, "compare0"); 
    break; 
    case 1: 
            usort($new_array, "compare1"); 
    break; 
    case 2: 
            usort($new_array, "compare2"); 
    break; 
} 

############################################################### 
#    We display the infomation here 
############################################################### 

$i2 = count($new_array); 
$i = 0; 
echo "<table border=1><tr><td width=150> File name</td><td width=100> File Size</td><td width=100>Last Modified</td></tr>"; 
for ($i=0;$i<$i2;$i++) { 
    if (!$do_link) { 
        $line = "<tr><td align=right>" . $new_array[$i][0] . "</td><td align=right>" . number_format(($new_array[$i][1]/1024)) . "k"; 
        $line = $line  . "</td><td align=right>" . $new_array[$i][3] . "</td></tr>"; 
    }else{ 
        $line = '<tr><td align=right><A HREF="' .  $new_array[$i][0] . '">' . $new_array[$i][0] . "</A></td><td align=right>"; 
        $line = $line . number_format(($new_array[$i][1]/1024)) . "k"  . "</td><td align=right>" . $new_array[$i][3] . "</td></tr>"; 
    } 
    echo $line; 
} 
echo "</table>"; 


?> 


 

Hope it of some help

 

Lite...

 

<?
$path = '/home/themungo/public_html/up/';
# Initialise list arrays, files and array counters for them
$t = 0;
$f = 0;
$images_arr['name'] = array();
$images_arr['time'] = array();
if (@$handle = opendir($path)) {
while (false!== ($file = readdir($handle))) {
if($file!= "." && $file!= "..") {
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)){
$images_arr['time'][$t++] = filemtime($file); //<---- here it is, just a seperate key in the array to store filetimes
$images_arr['name'][$f++] = $file;
};
};
};
closedir($handle);
arsort( $images_arr['time'] );
arsort( $images_arr['name'] );
}

//test
foreach ($images_arr['time'] as $key=>$ftime){
$fname = $images_arr['name'][$key];
echo '<strong>filename:</strong> '.$fname.' <strong>filetime:</strong> '.$ftime.'<br/>';
}?>

 

Ok that will display them in order... The last uploaded file is displayed first... Now how can i limit to "x" amount of results?

Change:

while (false!== ($file = readdir($handle))) {
if($file!= "." && $file!= "..") {
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)){
$images_arr['time'][$t++] = filemtime($file); //<---- here it is, just a seperate key in the array to store filetimes
$images_arr['name'][$f++] = $file;
};
};

 

To:

 

while ((false!== ($file = readdir($handle))) && $count < 20) {
if($file!= "." && $file!= "..") {
$fName = $file;
$file = $path.'/'.$file;
if(is_file($file)){
$images_arr['time'][$t++] = filemtime($file); //<---- here it is, just a seperate key in the array to store filetimes
$images_arr['name'][$f++] = $file;
};
};
$count++;
}

 

You can change the amount in the while loop if you want.

<?
$path = '/home/themungo/public_html/up';
# Initialise list arrays, files and array counters for them
$t = 0;
$f = 0;
$images_arr['name'] = array();
$images_arr['time'] = array();
if (@$handle = opendir($path)) {
	while ((false!== ($file = readdir($handle))) && $count < 15) {
		if($file!= "." && $file!= "..") {
		$fName = $file;
		$file = $path.'/'.$file;
			if(is_file($file)){
			$images_arr['time'][$t++] = filemtime($file); //
			$images_arr['name'][$f++] = $file;
		};
	};
	$count++;
};
closedir($handle);
arsort( $images_arr['time'] );
arsort( $images_arr['name'] );
}


foreach ($images_arr['time'] as $key=>$ftime){
$fname = $images_arr['name'][$key];
echo '<strong>filename:</strong> '.$fname.' <strong>filetime:</strong> '.$ftime.'<br/>';
}

?>

 

This however, gets the oldest 15 files and sorts them in order... Not grabs the newest 15... Any help?

It is listing newest first for me.

 

However, it seems to be skipping some files - curious.

 

Here's a slightly more complex solution..

 

Revert back to the original code without the $count stuff.

 

Change the test code at the end to this:

 

//test
$i = 1;
foreach ($images_arr['time'] as $key=>$ftime){
$fname = $images_arr['name'][$key];
echo '<strong>filename:</strong> '.$fname.' <strong>filetime:</strong> '.$ftime.' <strong>i:</strong> '.$i.' <br/>';
if ($i == 15) {
echo "qwerty";
break;
}
$i++;
}

I'm sure someone will suggest something better, though!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.