Hi,

This must be a rather dumb question, but I'll ask it anyway.

Me and my friend are making a simple news script for our website, and thus we are not going to use mysql. The idea was to use mod_rewrite for SEO friendly URLs e.g /news/2008/12/25/this-is-the-subject/ (== news.php?y=2008&m=12&d=25&s=this-is-the-subject )
The point is to make the script as light as possible, so which would be better:

1) Use file() to read one file that has all the news (per line), and check each array for the correct news.

2) Make each news a file (e.g [email]2008@12@p[/email]). Then read the file names to an array and check each array for the correct news.

Hope that makes some sense. Thanks.

Dani AI

Generated

Brief recap: is weighing a single aggregated file versus one file per news item. points out the memory hit of loading everything; shared a directory-scan script. Both patterns work — choose based on volume, concurrency needs, and how often you write vs read.

Single-file (pros/cons): simplest to implement and backup, but file() reads the whole file into memory which is fine for small archives and bad for large ones. If you keep this route, parse with fopen()/fgets() to stream instead of loading the entire history at once.

Per-file (pros/cons): reads for a single article are cheap and easy to cache. Downsides are directory scanning cost and filesystem limits when you have many files in one folder. Mitigation: organize by year/month subfolders and keep filenames as YYYY-MM-DD-slug.ext so lexical sorting = chronological sorting. Store data files outside webroot so they cannot be executed directly.

Concurrency and safety: always write atomically and use locking so readers never see partial files. Example write pattern:

$tmp = $dir . '/.tmp-' . uniqid('', true);
file_put_contents($tmp, $data, LOCK_EX);
rename($tmp, $final);   // atomic on most filesystems

For more control use fopen() + flock() and fflush() before rename().

Practical recommendation: for a small site (hundreds of items) use per-file storage + a tiny JSON or serialized index that you update on writes and cache for reads (avoids scanning every request). If you expect growth, switching to SQLite is a lightweight, zero-admin step that gives indexing, queries and safer concurrency without a full RDBMS. Always sanitize slugs, validate inputs, and cache listings so the script stays both light and reliable.

Recommended Answers

All 2 Replies

To have less of an impact on the server at runtime I'm guessing 2) would be much better as in 1) you must load the entire history of news items into memory as opposed to about 30 bytes per news item. (Have I interpreted you correctly?)

ob_start("ob_gzhandler"); 
$p = split('/', $_SERVER['SCRIPT_FILENAME']);
	$script_name = $p[count($p)-1];
	$path = str_replace($script_name, '', $_SERVER['SCRIPT_FILENAME']);
	$dir_handle = @opendir($path) or die("Unable to open $path");
Function get_Extension($m_FileName){
 	$path_parts = pathinfo($m_FileName);
 	if ($path_parts["extension"]) {
 		$m_Extension = strtolower($path_parts["extension"]);
 		return(strtoupper($m_Extension));
 		}
 	else { return "unknown"; }
 }
function check_image($filename){
 $temp=strtoupper(get_Extension($filename));
 if(($temp=="NEWS")||($temp=="PHP"))
 return (true);
 else
 return (false);
 }
Function get_Files($path) {
 	if ($handle = opendir($path)) {	
 		while (false !== ($file = readdir($handle))) { 
 		if(!is_dir($file) && substr($file,O,1) != "."){				
				$m_Files[]=$file;
 			}
 		}
 closedir($handle); 
 	}
 if(sizeof($m_Files)>1)
 asort($m_Files);
 return $m_Files;
 }
 $files=get_Files($path); 
 $filter_files=array_filter($files,"check_image");
 $maxnr=sizeof($filter_files)-1;
 sort($filter_files);
for ($i=0;$i<sizeof($filter_files);$i++){
	echo "<a href='$filter_files[$i]'>";
	echo substr($filter_files[$i], 0, strlen($filter_files[$i])-4);
	echo "</a><br />";
 }
closedir($dir_handle); 
ob_flush();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.