Tag Archives: modified time

PHP: Simple Script to Retrieve the Newest File in a Folder CodeUnit 31 OCT 2011

Not particularly efficient for very large folders of course, but this simple little script will successfully check the last modified timestamp of files in a folder to determine the newest most fresh file contained within, before forcing a page redirect to the file, useful if you say want to show for instance the latest image file contained in a directory.

//determing the latest file in a folder and redirecting to it

//get the correct file path to use
$documentRoot =$_SERVER['DOCUMENT_ROOT']; 
if (key_exists('SUBDOMAIN_DOCUMENT_ROOT',$_SERVER)){ 
	$documentRoot = $_SERVER['SUBDOMAIN_DOCUMENT_ROOT']; 
} 
//set the folder path to the target folder
$folder = $documentRoot . DIRECTORY_SEPARATOR . 'myfolder'; 

//variable to hold the newest file path
$newestFile = ''; 

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

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

Unable to determine latest file in the specified folder.

'; }

Like I said, not the most efficient of methods for very large folders, but simple enough to do the job for those smaller ones!

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'];

if (key_exists('SUBDOMAIN_DOCUMENT_ROOT',$_SERVER)){
	$documentRoot = $_SERVER['SUBDOMAIN_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=”http://comicserver.com/latestcomicimage.php” />

Nifty.