Presenting A Directory in PHP by Dylan Modesitt

In my opinion, WordPress, anchor, ghost, and anything of the like of far too over-thought, and over-coded. I wanted to create a simple blogging solution. I manage this personal blog with some simple PHP that relies on some very basic ideals. I want full power over my posts; I want them to be html pages that I create without any hindrance to their styling and appearance. In reality, I just wanted an automatic way to sort through a directory that I deemed as the location of all my content, and display every file in that directory in a ‘blogesc’ fashion. I did so with the following code:

			    
			       <?php
				        	$dir = 'articles/';
				            function page_title($url) {
				                $fp = file_get_contents($url);
				                if (!$fp) 
				                    return null;
				                $res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
				                if (!$res) 
				                    return null; 
				                $title = preg_replace('/\s+/', ' ', $title_matches[1]);
				                $title = trim($title);
				                return $title;
				            }
				            function scan_dir($dir){
				                $ignored = array('.', '..', '.svn', '.DS_Store');

				                $files = array();    
				                foreach (scandir($dir) as $file) {
				                    if (in_array($file, $ignored)) continue;
				                    $files[$file] = filemtime($dir . '/' . $file);
				                }
				                arsort($files);
				                $articles = array();
				                foreach ( $files as $key => $value ) {
				                    $articles[] = $key;
				                }
				                $articlesTitle = array();
				                $articlesTime = array();
				                $articlesDescription = array();
				                foreach ( $articles as $key => $value ) {
				                    $HTMLInString = file_get_contents($dir . $value);
				                    $start = strpos($HTMLInString, '<p>');
				                    $end = strpos($HTMLInString, '</p>', $start);
				                    $paragraph = substr($HTMLInString, $start, $end-$start+4);

				                    $HTMLtitle = page_title($dir . $value);
				                    $articlesTitle[] = $HTMLtitle;
				                    $articlesTime[] = date ("F d Y", filemtime($dir . $value));
				                    $articlesDescription[] =  $paragraph;
				                }

				                for ($x = 0; $x < count($articles); $x++) {
				                    echo "<div class='col-xs-12 articleSample'><h1 class='articleTitle'><a href='articles/$articles[$x]'>$articlesTitle[$x]</a></h1><p> By Dylan Modesitt on $articlesTime[$x]</p> $articlesDescription[$x] <a href='articles/$articles[$x]'>Read More...</a></div>";
				                }

				            }
				            scan_dir($dir);
				        ?>
			    
			  

This set of PHP will scan a directory for html files, then format them accordingly and present them in a minimal manner according to my style sheet. The end result of this creation is something I thought was quite cool; automatic listing of a directory on your site. I am sure this has been done before; most things have. However, I thought I would share my version here. This could be used in many ways: listing images, files, making a blog, and beyond.



lex parsimoniae