www.webdeveloper.com
Results 1 to 11 of 11

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

  1. #1
    Join Date
    Jun 2008
    Posts
    64

    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) {

    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
    This one lists all the files (and are clickable):

    PHP Code:
     if ($handle opendir('.')) {
       while (
    false !== ($file readdir($handle)))
          {
              if (
    $file != "." && $file != "..")
          {
                  
    $thelist .= '<a href="/news/'.$file.'">'.$file.'</a>';
              }
           }
      
    closedir($handle);
      } 
    Any way to combine the two and sort by modified (newest first)?

    Thanks.

  2. #2
    Join Date
    Jun 2008
    Posts
    64
    Hey, I think I got one part:

    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) {

    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
    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...
    Last edited by stingerman; 01-23-2010 at 12:43 AM.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Here's a way to get an array of all non-directory files in a directory, sorted newest to oldest:
    PHP Code:
    <?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";
    }
    "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

    eBookworm.us

  4. #4
    Join Date
    Jun 2008
    Posts
    64
    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?

    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) {

    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

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    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

    eBookworm.us

  6. #6
    Join Date
    Jun 2008
    Posts
    64
    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)?

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    After doing the sort:
    PHP Code:
    $files array_slice($files010); 
    "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

    eBookworm.us

  8. #8
    Join Date
    Jun 2008
    Posts
    64
    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!

  9. #9
    Join Date
    Jun 2008
    Posts
    64
    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?

    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 03:18 PM.

  10. #10
    Join Date
    May 2010
    Location
    Washington, DC region
    Posts
    6

    More than one directory?

    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?

  11. #11
    Join Date
    Aug 2012
    Posts
    1
    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!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles