Page 1 of 1

Tweaking directory contents display script

Posted: Mon Jun 29, 2009 7:04 pm
by msaz87
I'm new to PHP and looking for someone to help me modify a script that displays the five newest files in a directory. The script also strips the file extension and pretties it up by replacing underscores, etc.

The original script is below... but the problem is I was hoping to find a way to define the directory it displayed the contents of:

Code: Select all

 
<?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";
?>
 
I am able to control the directory in the below example... but the new issue is that

It used to look like:
File 1 - Jun 28, 2009

And now it looks like:
./files/file_1.htm - Jun 28, 2009

Code: Select all

 
<?php
 
$show = 5;
$dir = './DIRECTORY/';
 
$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";
?>
 
My only concern is really stripping off the path

Any help is appreciated -- thanks in advance!

Re: Tweaking directory contents display script

Posted: Mon Jun 29, 2009 7:59 pm
by requinix
Go back to your earlier working code and give it another shot. All you needed to do was (a) get the listing from a specific directory, and (b) only keep the path name from it. You ended up making it more complicated than it needs to be.

In all fairness though, you probably didn't know about the basename function.

Code: Select all

<?php
 
$show = 5;
$dir = "./DIRECTORY/"
 
$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 ) {
    $file = basename($files[$i]);
    echo '<a href="', $file, '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ), '</a> - ', date( 'D, M d, Y', filemtime($file) ), '<br />', "\n";
}
?>

Re: Tweaking directory contents display script

Posted: Mon Jun 29, 2009 8:14 pm
by msaz87
tasairis wrote:Go back to your earlier working code and give it another shot. All you needed to do was (a) get the listing from a specific directory, and (b) only keep the path name from it. You ended up making it more complicated than it needs to be.

In all fairness though, you probably didn't know about the basename function.

Code: Select all

<?php
 
$show = 5;
$dir = "./DIRECTORY/"
 
$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 ) {
    $file = basename($files[$i]);
    echo '<a href="', $file, '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ), '</a> - ', date( 'D, M d, Y', filemtime($file) ), '<br />', "\n";
}
?>
Thanks for the help -- your code works but spits out a new error:

Code: Select all

 
Podcast_itunes - Warning: filemtime() [function.filemtime]: stat failed for podcast_itunes.jpg in /PATH../../test.php on line 11
Wed, Dec 31, 1969
 
Thanks for your assistance!

Re: Tweaking directory contents display script

Posted: Mon Jun 29, 2009 10:37 pm
by requinix
Ain't my code - I just changed yours by a tiny bit.

You getting the error for every file or just that one?

Re: Tweaking directory contents display script

Posted: Mon Jun 29, 2009 10:46 pm
by msaz87
It seems to happen on every file, regardless of thge directory

Re: Tweaking directory contents display script

Posted: Mon Jun 29, 2009 11:02 pm
by requinix
Ah.
(coughsomaybeitwasmycodeafterall>_>;)

Code: Select all

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

Re: Tweaking directory contents display script

Posted: Tue Jun 30, 2009 12:17 am
by msaz87
tasairis wrote:Ah.
(coughsomaybeitwasmycodeafterall>_>;)

Code: Select all

for ( $i = 0; $i < $show; ++$i ) {
&nbsp; &nbsp; $file = basename($files[$i]);
&nbsp; &nbsp; echo '<a href="', $file, '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ), '</a> - ', date( 'D, M d, Y', filemtime($files[$i]) ), '<br />', "\n";
}
Works fantastic now... thanks!