Re: Order directory output

From: Date: Fri, 08 Feb 2008 23:44:17 +0000
Subject: Re: Order directory output
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-268995@lists.php.net to get a copy of this message
Pastor Steve wrote:
Hi, thanks for all your help today. I have the following code and I am trying to order the output. Currently it seems really random. Can anyone point me in the right direction? <?php $dir = "content/current/breaking_news/"; // set pattern $pattern = ".txt*|.TXT*"; // open directory and parse file list if (is_dir($dir)) { if ($dh = opendir($dir)) { //This is the div that contains the wrap around for the breaking news section. echo "
    <div class=\"spstory\" style=\"font-family: Times New Roman, Times,
serif; font-size: 12px; width: 290px;\">
        <div style=\"width: 285px; background-color: #FFFFCC; padding:
3px;\">
            <span class=\"NormalHeadRed\">Breaking News</span>
            <br />Please check here often for breaking news stories.
        </div>
            <p />
        <span class=\"NomalText\">
                <ul>";
// iterate over file list while (($filename = readdir($dh)) !== false) { // if filename matches search pattern, print it if (ereg($pattern, $filename)) if(strpos($filename,'.')>0) { $fh = fopen("$dir" . "$filename", "r");
    $filehead = fgets($fh);
fclose($fh); echo "
        <li class=\"bn_bullet\">
            <a href=\"/breaking_news/$filename\">$filehead</a>
        </li>";
} } echo "
        </ul><p />
        </span>
    </div>";
// close directory closedir($dh); } } ?> Thank you, -- Steve Marquez
A cut down version of what you are trying to do, I think. <?php $dir = "../../"; // set pattern $allowed_ftypes = array('php','txt','TXT'); // open directory and parse file list if (is_dir($dir)) {
    if ( ($files = glob($dir.'*') ) !== false ) {
        // This is the div that contains the wrap around for the breaking news
// section.
        echo '<div>
            Breaking News<br />
            Please check here often for breaking news stories.
            <p>
            <span class="NomalText">
            <ul>';
        // iterate over file list
        foreach ( $files AS $file ) {
            // if filename matches search pattern, print it
            if ( in_array(array_pop(explode('.', $file) ), $allowed_ftypes ) ) {
                // Open file for reading
                if ( ( $fh = fopen($file, "r") ) !== false ) {
                    // Get the first line of the file
                    $filehead = fgets($fh);
                    // Extract file name for link
                    $filename = array_pop(explode('/', $file));
                    // Display Link
                    echo '<li><a href="/breaking_news/'.$filename.'">'.
                              htmlspecialchars($filehead).'</a></li>';
                    fclose($fh);
                }
            }
        }
        echo '</ul></p></span></div>';
    }
} ?> -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#268995) next »