msaz87 Posted June 29, 2009 Share Posted June 29, 2009 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! Link to comment https://forums.phpfreaks.com/topic/164072-help-with-tweak-on-directory-contents-display-script/ Share on other sites More sharing options...
Mark Baker Posted June 29, 2009 Share Posted June 29, 2009 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 Link to comment https://forums.phpfreaks.com/topic/164072-help-with-tweak-on-directory-contents-display-script/#findComment-865523 Share on other sites More sharing options...
dzelenika Posted June 29, 2009 Share Posted June 29, 2009 <?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"; ?> Link to comment https://forums.phpfreaks.com/topic/164072-help-with-tweak-on-directory-contents-display-script/#findComment-865525 Share on other sites More sharing options...
msaz87 Posted June 29, 2009 Author Share Posted June 29, 2009 <?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 Link to comment https://forums.phpfreaks.com/topic/164072-help-with-tweak-on-directory-contents-display-script/#findComment-865531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.