+ Reply to Thread
Results 1 to 5 of 5

Thread: PHP to display recent files.

  1. #1
    brigham is offline x10Hosting Member brigham is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    11

    PHP to display recent files.

    I would like for the homepage of a site to display only the three most recent news items or updates in chronological order. I'm currently using php to reference a directory that contains all news updates as individual flat text files. Each file is named for the date it was created (20090722 would be a news item created on 22 July, 2009).

    The script looks into the directory and selects the first three files encountered to be included on the home page. The files are named by their creation date which will result in a logical order.

    Here's the script that calls and displays the three most recent updates.
    PHP Code:
    <?php
    if ($handle opendir('updates/')) { 
          
    $i 0;
          while((
    $file readdir($handle)) && $i 3) {   // Limits the script to display only
               
    if ($file != "." && $file != "..") {     // 3 items if they aren't directory references
                    
    include ("updates/$file");                               
                    
    $i++;
                  }
            }
        
    closedir($handle);
        }
    ?>
    Is there a better way to accomplish this? Are there security problems with this? Thanks for any help.
    Last edited by brigham; 08-22-2009 at 11:52 PM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP to display recent files.

    The files returned by readdir are in filesystem order, which might be sorted by name (in ascending order--you want descending) but can just as easily be by creation date or an arbitrary order. X10 runs off of Linux and currently uses ext3; I believe entries in ext3 directories are unsorted. scandir will return the contents of a directory as an array of filenames sorted in ascending or descending order.

    PHP Code:
    $news array_slice(scandir('updates'1), 03); 
    If you have a large number of news items, the above will get unwieldy as scandir (or any implementation that scans the directory) will need to sort the list of files each time the page is opened. You could cache the three most recent files in another file (either in a different directory, or in 'updates' but lexically lower than the news items, e.g. '.recent.txt' or '!recent.txt'), updating it when its modification time is older than the directory's modification time.

    Alternatively, store the news items in a database.
    Code:
    CREATE TABLE news (
      id INTEGER PRIMARY KEY AUTO_INCREMENT,
      date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      title VARCHAR(64) NOT NULL,
      subtitle VARCHAR(128) DEFAULT NULL,
      body VARCHAR(65024) NOT NULL, -- could also make body TEXT
      INDEX (date)
    );
    Statement to store values:
    Code:
    INSERT INTO news (title, body) VALUES ('Sticks Nix Hick Pix', 'Current visit to Hollywood...'), ... ;
    Note that you don't need to specify the date when inserting; news items are automatically dated upon insert. Statement to retrieve the 3 most recent news items:
    Code:
    SELECT date,title,subtitle,body  FROM news ORDER BY date DESC LIMIT 3;
    Just make sure you use PDO and prepared statements if you go this route.

    Another option would be to put all the news items into a single file, adding new items to the top and separating items with some unique string that won't appear in the items. To make sure of this, you can prefix a space to any line in a news item that happens to be the separator and strip spaces from lines (after testing for the separator).

    Performance-wise, the single file is probably the fastest, as long as you don't need any other operations on news items. If you need the n-th most recent news item, for instance, the database will probably be fastest.

    As for security, you mostly need to worry about user input. It's rather hard to exploit code that behaves the same no matter what input it's sent. Outside security holes in scripts, there's one other potential hole: the files containing the news items in your implementation will be directly readable by visitors. If that's a problem, just move 'updates' outside the DOCUMENT_ROOT hierarchy.
    Last edited by misson; 08-23-2009 at 04:05 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP to display recent files.

    As always, mission provides a great and very well explained answer !

    Keep in mind, and I speak out of personal experience, that using flatfile systems may sound like the easiest, but the problem is that there could be performance reductions (what if all the users of a servers want to access a file at the same time). When I moved to only using database, I found that my site was responding faster, and the data was safer as well.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: PHP to display recent files.

    PHP Code:
    if ($handle opendir('updates/')) { 
          
    $i 0;
          
    $allfiles = array();
          while((
    $file readdir($handle)) ) {  
               if (
    $file != "." && $file != "..") {   
                    
    $allfiles[] =  $file ;    
                }
          }
          
    closedir($handle);

          
    rsort$allfiles );  # sort names in reverse order  
          
    while( $i ){
             
    $to_include 'updates/' $allfiles[$i] ;
              include (
    "$to_include"); 
             
    $i++ ;
          }

    Last edited by descalzo; 08-23-2009 at 03:34 PM. Reason: too sleepy
    Nothing is always absolutely so.

  5. #5
    brigham is offline x10Hosting Member brigham is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    11

    Question resolved. Thanks!

    Thanks so much for the help. I was trying to avoid using tables (no real reason), but I guess I'll quit being silly about it.

    Thanks again to all three responders.

+ Reply to Thread

Similar Threads

  1. Using Include Files with html on X10Hosting
    By frankfriend in forum Tutorials
    Replies: 0
    Last Post: 07-06-2009, 01:38 PM
  2. PHP files won't load
    By AlexLH in forum Free Hosting
    Replies: 6
    Last Post: 08-28-2008, 08:54 AM
  3. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  4. I need php code for downloading zip files from my site !!
    By careerbridge in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 07-16-2006, 12:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers