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.
PHP Code:
///////////////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) {
///////////////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) {
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
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?
PHP Code:
///////////////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) {
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):
PHP Code:
// 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";
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
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?
PHP Code:
clearstatcache();
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.
Last edited by stingerman; 01-23-2010 at 02:18 PM.
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?
Bookmarks