Tweaking directory contents display script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
msaz87
Forum Newbie
Posts: 4
Joined: Mon Jun 29, 2009 6:57 pm

Tweaking directory contents display script

Post 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!
Last edited by Benjamin on Tue Jun 30, 2009 12:10 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Tweaking directory contents display script

Post 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";
}
?>
msaz87
Forum Newbie
Posts: 4
Joined: Mon Jun 29, 2009 6:57 pm

Re: Tweaking directory contents display script

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Tweaking directory contents display script

Post 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?
msaz87
Forum Newbie
Posts: 4
Joined: Mon Jun 29, 2009 6:57 pm

Re: Tweaking directory contents display script

Post by msaz87 »

It seems to happen on every file, regardless of thge directory
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Tweaking directory contents display script

Post 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";
}
msaz87
Forum Newbie
Posts: 4
Joined: Mon Jun 29, 2009 6:57 pm

Re: Tweaking directory contents display script

Post 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!
Post Reply