Skip to content

Instantly share code, notes, and snippets.

@jgraup
Last active December 29, 2015 07:59
Show Gist options
  • Save jgraup/7640422 to your computer and use it in GitHub Desktop.
Save jgraup/7640422 to your computer and use it in GitHub Desktop.
DirectoryContent - Return list of files and folders in directory including path and url.
<?php
/**
* DirectoryContent Class
*/
class DirectoryContent
{
/**
* @var $filename string
* @var $path string
* @var $url string
* @var $extension string
*/
var $filename, $path, $url, $extension;
// constructor
function DirectoryContent() {
return true;
}
/**
* @param $ext string Extension to look for.
* @return bool if found
*/
function hasExtension($ext){
return isset($this->extension) && strpos(strtolower($this->extension), strtolower($ext)) !== FALSE;
}
/**
* @return int Size in bytes.
*/
function filesize(){
return isset($this->filename) && file_exists($this->filename) ? filesize($this->filename) : 0;
}
}
/**
* DirectoryContents Class
*/
class DirectoryContents
{
/**
* @var $error Exception included if there was a parsing error.
* @var $files array Files found at path.
* @var $folders array Folders found at path.
*/
var $error;
var $files = array();
var $folders = array();
// constructor
function DirectoryContents() {
return true;
}
/**
* Get list of files and folders in current directory.
* @return DirectoryContents
*/
public static function GetCurrentDirectoryContents()
{
/**
* @var $dir DirectoryContents
* @var $REQUEST_URI string
* @var $path string
* @var $baseDir string
* @var $inx int
*/
$dir = new DirectoryContents();
$path = getcwd();
try {
if ($handle = opendir($path))
{
// try to get a base url for asset links
$REQUEST_URI = $_SERVER["REQUEST_URI"];
$inx = strrpos($REQUEST_URI, "/");
if ( $inx === 0 ) $inx = strlen($REQUEST_URI);
$baseDir = substr($REQUEST_URI, 0, $inx);
if ((strlen($_SERVER["DOCUMENT_ROOT"]) === 0) ||
strpos($_SERVER["HTTP_HOST"], "localhost:") !== FALSE ) {
$pathUri = "http://" . $_SERVER["HTTP_HOST"] . $baseDir . "/";
}
else
{
$pathUri = $_SERVER["SERVER_NAME"] . $baseDir . "/";
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'
|| $_SERVER['SERVER_PORT'] == 443) {
$pathUri = "https://" . $pathUri;
}
else $pathUri = "http://" . $pathUri;
}
// cleanup
$pathUri = str_replace("///", "//", $pathUri);
$cleanPath = (substr($path,"/",-1) === "/") ? $path : $path . "/";
while (false !== ($file = readdir($handle)))
{
// data
$content = new DirectoryContent();
// file
if ($file != "." && $file != ".." && !is_dir($file) )
{
$content->url = $pathUri . $file;
$content->path = $cleanPath . "/" . $file;
$content->filename = $file;
$content->extension = substr(strrchr($file, "."), 1);
$dir->files[] = $content;
}
// folder
else
{
$content->url = $pathUri . $file;
$content->path = $cleanPath . $file;
$content->filename = $file;
$dir->folders[] = $content;
//$folder = @dir($file);
//if ($folder) $dir->folders[] = $folder;
}
}
closedir($handle);
}
// sort
sort($dir->files);
sort($dir->folders);
}
catch (Exception $e) {
$dir->error = $e;
}
return $dir;
}
}
<html><head></head><body>
<?php
require_once ("directory-contents.php");
/**
* @var $dirContents DirectoryContents
* @var $entry DirectoryContent
*/
$dirContents = DirectoryContents::GetCurrentDirectoryContents();
foreach($dirContents->files as $entry)
{
echo '<a href="' . $entry->url . '" title="' . ($entry->hasExtension("JPG")?"JPG":"FILE") . '" >';
echo $entry->filename, "\t\t\t", $entry->filesize(), "\n";
echo "</a><br>\n";
}
foreach($dirContents->folders as $entry)
{
echo '<a href="' . $entry->url . '" >';
echo $entry->filename, "\n";
echo "</a><br>\n";
}
?>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment