/    Sign up×
Community /Pin to ProfileBookmark

Read files in directory and list title w/url in modified order?

I have a bunch of files in my ‘news’ folder and would like to have another page (archive page) that lists them by title (with the title linked) with the newest title listed first.

I did find the following php code that lists specific file titles in a directory, however not sure what to add to sort and make the title linkable.

[code=php]///////////////function my_strip///////////
function my_strip($start,$end,$total){
$total = stristr($total,$start);
$f2 = stristr($total,$end);
return substr($total,strlen($start),-strlen($f2));
}
/////////////////////End of function my_strip ///
///////////// Reading of file content////
$i=0;
$path=”../news/”;// Right your path of the file

$handle=opendir($path);
while (($file_name = readdir($handle))!==false) {

if(stristr($file_name,”.php”)){
$url=$path.$file_name;

$contents=””;
$fd = fopen ($url, “r”); // opening the file in read mode
while($buffer = fread ($fd,1024)){
$contents .=$buffer;
}
/////// End of reading file content ////////
//////// We will start with collecting title part ///////
$t=my_strip(“<title>”,”</title>”,$contents);
echo $t;
echo “<br>”;
$i=$i+1;
}
}
echo $i;[/code]

This one lists all the files (and are clickable):

[code=php] if ($handle = opendir(‘.’)) {
while (false !== ($file = readdir($handle)))
{
if ($file != “.” && $file != “..”)
{
$thelist .= ‘<a href=”/news/’.$file.'”>’.$file.'</a>’;
}
}
closedir($handle);
}[/code]

Any way to combine the two and sort by modified (newest first)?

Thanks.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@stingermanauthorJan 23.2010 — Hey, I think I got one part:

[code=php]///////////////function my_strip///////////
function my_strip($start,$end,$total){
$total = stristr($total,$start);
$f2 = stristr($total,$end);
return substr($total,strlen($start),-strlen($f2));
}
/////////////////////End of function my_strip ///
///////////// Reading of file content////
$i=0;
$path="../news/";// Right your path of the file

$handle=opendir($path);
while (($file_name = readdir($handle))!==false) {

if(stristr($file_name,".php")){
$url=$path.$file_name;

$contents="";
$fd = fopen ($url, "r"); // opening the file in read mode
while($buffer = fread ($fd,1024)){
$contents .=$buffer;
}
/////// End of reading file content ////////
//////// We will start with collecting title part ///////
$t=my_strip("<title>","</title>",$contents);
$thelist = '<a href="/news/'.$file_name.'">'.$t.'</a>';
echo $thelist;
echo "<br>";$i=$i+1;
}
}
echo $i;[/code]


Now to find out how to sort...

*Actually this looks like it does sort, however the newest files/titles are at the bottom of the page. Need them first...
Copy linkTweet thisAlerts:
@NogDogJan 23.2010 — Here's a way to get an array of all non-directory files in a directory, sorted newest to oldest:
[code=php]
<?php
$files = glob('path/to/files/*');
$files = array_filter(
$files,
create_function(
'$file',
'return (!is_dir($file));'
)
);
usort(
$files,
create_function(
'$a,$b',
'return filemtime($b) - filemtime($a);'
)
);
foreach($files as $file)
{
echo date('Y-m-d H:i:s', filemtime($file)) . " - " . basename($file) . "<br />n";
}
[/code]
Copy linkTweet thisAlerts:
@stingermanauthorJan 23.2010 — Thanks for replying.

That looks like another piece of the puzzle...

However, I still need to combine that with the other...somehow ?

...As I don't want the filenames returned but the html title tags (of the php files) in order modified.

Almost there I guess.

Suppose somehow I need to make an array out of this to sort?

[code=php]///////////////function my_strip///////////
function my_strip($start,$end,$total){
$total = stristr($total,$start);
$f2 = stristr($total,$end);
return substr($total,strlen($start),-strlen($f2));
}
/////////////////////End of function my_strip ///
///////////// Reading of file content////
$i=0;
$path="../news/";// Right your path of the file

$handle=opendir($path);
while (($file_name = readdir($handle))!==false) {

if(stristr($file_name,".php")){
$url=$path.$file_name;

$contents="";
$fd = fopen ($url, "r"); // opening the file in read mode
while($buffer = fread ($fd,1024)){
$contents .=$buffer;
}
/////// End of reading file content ////////
//////// We will start with collecting title part ///////
$t=my_strip("<title>","</title>",$contents);
$thelist = '<a href="/news/'.$file_name.'">'.$t.'</a>';
echo $thelist;
echo "<br>";$i=$i+1;
}
}
echo $i; [/code]
Copy linkTweet thisAlerts:
@NogDogJan 23.2010 — The idea with the code I provided was that you could then use the $files array to drive the rest of the process, probably via a foreach() loop, something like (untested):
[code=php]
// code that creates/sorts $files from previous reply, then...
echo "<ul>n";
foreach($files as $file)
{
$text = file_get_contents($file);
if(preg_match('#<title>(.*)</title>#is', $text, $matches))
{
echo "<li><a href='/news/".basename($file)."'>".$matches[1]."</a></li>n";
}
}
echo "</ul>n";
[/code]
Copy linkTweet thisAlerts:
@stingermanauthorJan 23.2010 — That worked!

Nice!

Thanks a lot.

Think I spend three or four hours last night fooling around with various codes trying to figure it out,lol.

Hmmm.. any way to display, say, the first ten listed (last modified)?
Copy linkTweet thisAlerts:
@NogDogJan 23.2010 — After doing the sort:
[code=php]
$files = array_slice($files, 0, 10);
[/code]
Copy linkTweet thisAlerts:
@stingermanauthorJan 23.2010 — Hmmm... returns 7 for some reason, but when set to 0,13 returns 10.

Either, way, exactly what I have been looking for.

Thank you very much, sir!
Copy linkTweet thisAlerts:
@stingermanauthorJan 23.2010 — Question -

When I upload and/or modify a new file the script doesn't return it - only shows files from the moment the script has been uploaded.

Do I need to clearstatcache somewhere?

[code=php]clearstatcache();[/code]

Thanks.

*Scratch the above.

I have the title tags in a php include file and use php echo. The script doesn't read the includes, I guess (I assumed it read it as a browser would). I just need to have the title tags in the page the script reads.
Copy linkTweet thisAlerts:
@MadMac10May 14.2010 — Hi!

In my case, old articles are archived according to year. How would I adjust this code so that it will look in several directories in different locations?
Copy linkTweet thisAlerts:
@dexfourtyAug 22.2012 — The script is fantastic, it's working really fine.

Could somebody help me how to get that list sorted by either <title> or url, alphabetically?

Thank you so very much!
×

Success!

Help @stingerman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...