Image

Imagepatchwolf wrote in Imagephp

fopen and fwrite help??

So, I've gotten a script on one subdomain of mine to write (append) to a file on another subdomain of mine (even though is_writable said I couldn't do it)...

This is all well and good, and it will serve, but it's not perfect

I'm using this script to add contents to the file, each one timestamped.

* 2006-08-22: somecontent
* 2006-08-23: somemorecontent


The problem thing I'd like to improve is that I'd like to sort the items in descending date order (newest items first).


if ($postresults == 'yes') { // add the result to a file
		$filename = '/path/to/file.txt';
		
		// because I'm opening $filename in append mode,
		// the file pointer is at the bottom of the file hence
		// that's where $posttext will go when we fwrite() it.
		if (!$handle = fopen($filename, 'a')) {
			echo "Cannot open file ($filename).";
			break;
		}
		
		// Write $posttext to our opened file.
		if (fwrite($handle, $posttext) === FALSE) {
			echo "Cannot write to file ($filename).";
			break;
		}
		
		echo "Success, wrote ($posttext) to file ($filename)";
		
		fclose($handle);
	}


The file already has some static content which will not change at the top of the file.


<!-- Start Static Content -- this needs to always remain at the top of the file -->
====== Title of Page ======

Explanatory paragraph...

===== Results =====
<!-- end static content -->
<!-- I'd like the dynamic content to start here, with newer items pushing the older ones down the page... -->
* 2006-08-23: somemorecontent
* 2006-08-22: somecontent


I'm guessing I'd need to use fseek(), but fseek warns me that if I open the file in append mode ("a" or "a+"), then it will be written at the end of the file regardless.

Any help?