Jump to content

Help with tweak on directory contents display script


msaz87

Recommended Posts

I found this existing script that displays the five newest files in a directory, but the only thing I'm having trouble with is finding a way to define the directory it's displaying. As it is, it simply displays the files of the directory it's in.

 

<?php

$show = 5;

$files = glob( '*.{html,php,php4,txt}', GLOB_BRACE );
usort( $files, create_function('$b, $a', 'return filemtime( $a ) - filemtime( $b );') );

for ( $i = 0; $i < $show; ++$i )
    echo '<a href="', $file = $files[$i], '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ), '</a> - ', date( 'D, M d, Y', filemtime($file) ), '<br />', "\n";
?>

 

Any help is appreciated... thanks in advance!

Either use getcwd() to get a record of the current directory, chdir to the directory you want, execute the list code, then chdir back to the saved directory

 

of modify the glob( '*.{html,php,php4,txt}', GLOB_BRACE ) to include the directory path

<?php

$show = 5;

$files = glob( 'c:\path\*.{html,php,php4,txt}', GLOB_BRACE );
usort( $files, create_function('$b, $a', 'return filemtime( $a ) - filemtime( $b );') );

for ( $i = 0; $i < $show; ++$i )
    echo '<a href="', $file = $files[$i], '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ), '</a> - ', date( 'D, M d, Y', filemtime($file) ), '<br />', "\n";
?>

<?php

$show = 5;

$files = glob( 'c:\path\*.{html,php,php4,txt}', GLOB_BRACE );
usort( $files, create_function('$b, $a', 'return filemtime( $a ) - filemtime( $b );') );

for ( $i = 0; $i < $show; ++$i )
    echo '<a href="', $file = $files[$i], '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ), '</a> - ', date( 'D, M d, Y', filemtime($file) ), '<br />', "\n";
?>

 

I took what you did and modified it slightly...

 

<?php

$show = 5;
$dir = './includes/';

$files = glob( ''.$dir.'*.{html,php,php4,txt}', GLOB_BRACE );
usort( $files, create_function('$b, $a', 'return filemtime( $a ) - filemtime( $b );') );

for ( $i = 0; $i < $show; ++$i )
    echo '<a href="', $file = $files[$i], '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ),$file, '</a> - ', date( 'D, M d, Y', filemtime($file) ), '<br />', "\n";
?>

 

But what happens is that now the script displays the path and extension. The original script stripped both these things and also replaced underscores, etc.

 

It used to look like:

File 1 - Jun 28, 2009

 

And now:

./files/file_1.htm - Jun 28, 2009

 

The only thing I really care about is stripping the path...

 

Thanks for the help -- I'm very new to PHP

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.