Mega Code Archive

 
Categories / Php / File Directory
 

Another script to display news from files in a directory

This version does not use the UNIX ls command and works also on Windows NT <html> <head> <title>News</title> </head> <body bfcolor="#ffffff"> <h1>News</h1> <div align="center"> <table border="0" cellspacing="5" cellpadding="10" width="90%" bgcolor="#e0e0e0"> <?php function createurl($text) { // Insert URL into text // Use the following syntax: ${http://mysite.ch} // Or: ${http://mysite.ch|My homepage in Swiss} $s = $text; $a=strstr($s, '${'); if ($a) { $b=strstr($a, '}'); if ($b) { $la=strlen($a); $ls=strlen($s); $s=substr($s,0,$ls-$la); $a=substr($a,2); $lb=strlen($b); $la=strlen($a); $a=substr($a,0,$la-$lb); $b=substr($b,1); $ta=strstr($a, "|"); if ($ta) { $la=strlen($a); $lt=strlen($ta); $linktext=substr($a,$la-$lt+1); $a=substr($a,0,$la-$lt); } else { $linktext=$a; } $s=$s. "<a href=\"".$a. "\">".$linktext. "</a>".$b; } } return($s); } // change this to the directory of your news files // they should be plain ASCII text files with extension ".txt" $newspath = "c:\\apache\\htdocs\\news\\"; // Declare array to hold filenames $newsfile = array(); // Create handle to search directory $newspath for files $hd = dir($newspath); // Get all files and store them in array while( $filename = $hd->read() ) { $s=strtolower($filename); if (strstr($s, ".txt")) { // Determine last modification date $lastchanged=filemtime($newspath.$filename); $newsfile[$filename] = $lastchanged; } } // Sort files in descending order arsort($newsfile); // Output files to browser for(reset($newsfile); $key = key($newsfile); next($newsfile)) { $fa = file($newspath.$key); $n=count($fa); print "<tr><td>\n"; print "<b>".date( "d.m.Y - H:i:s",$newsfile[$key]). "</b><br>\n"; for ($i=0; $i<$n; $i=$i+1) { $s=chop($fa[$i]); $s=htmlspecialchars($s); $s=createurl($s); print $s. "<br>\n"; } print "</td></tr>"; } $hd->close(); ?> </table> </div> </body> </html>