PHP and ComicPress: Simple way to Retrieve the Latest Comic Image CodeUnit 17 OCT 2011

I have a WordPress/ComicPress-driven web comic site and wanted to show the latest posted comic image on another one of my websites. Because I couldn’t locate anything to return just the latest image for embedding purposes, I quickly whipped up the following PHP script to achieve this for me:

//determing the latest image in the comics folder and redirecting to it if calculated

//get the correct file path to use
$documentRoot =$_SERVER['DOCUMENT_ROOT'];


//set the folder path to the comics folder
$folder = $documentRoot . DIRECTORY_SEPARATOR . 'comics';

//variable to hold the newest image path
$newestImage = '';

//loop through the comics directory. If file, compare the modified time to the currently set newest image. Update accordingly.
if (is_dir($folder)){
	foreach (scandir($folder) as $node) {
		$nodePath = $folder . DIRECTORY_SEPARATOR . $node;
		if (!is_dir($nodePath)){
		    if ($newestImage == ''){
		        $newestImage = $nodePath;
		    } else {
		        if (filemtime($nodePath) > filemtime($newestImage)){
			$newestImage = $nodePath;

//force redirect to the latest file, otherwise print an error message
if (is_file($newestImage)){
	header('location: ' . str_replace($documentRoot . DIRECTORY_SEPARATOR . 'comics','http://' . $_SERVER['SERVER_NAME'] . '/comics',$newestImage));
} else {
	echo '

Unable to determine latest image file.

'; }

Dump the above into a file and place it on your ComicPress server, before simply referencing it in an image tag on your target server. In other words, having saved the above to a file called latestcomicimage.php:

<img src=”” />


Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • Short, sweet, to the point, FREE-eaxclty as information should be!