Skip to content

Instantly share code, notes, and snippets.

@ankitnetwork18
Created January 11, 2013 07:24
Show Gist options
  • Save ankitnetwork18/4508687 to your computer and use it in GitHub Desktop.
Save ankitnetwork18/4508687 to your computer and use it in GitHub Desktop.
php: functions to read files from directory and filter them
/**
* list all files in a directory & subdirectory
* @param string $directory path to dir
* @param boolean $recursive weather recurisve listing or not
* @return array list of contents in dir
*/
function directoryToArray($directory, $recursive=false) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
/**
* this function filters an array of files for video files
* @param array $filesArray array of files
* @return array filtered video files
*/
function filter_video_files($filesArray) {
$files = array();
if(is_array($filesArray) && count($filesArray)>0) {
foreach($filesArray as $file) {
if(strstr($file, '.flv') || strstr($file, '.mp4')) {
$files[] = $file;
}
}
return $files;
}
return false;
}
// this function generates post title from file name
function get_title_from_file($file) {
$fileName = explode("/", $file);
$fileCount = count($fileName);
if($fileCount>1){
$last = $fileCount-1;
} else {
$last = 0;
}
$fileName = $fileName[$last];
$fileName = explode(".", $fileName);
$fileName = $fileName[0];
$fileName = explode("_",$fileName);
$fileName = implode(" ", $fileName);
$fileName = ucwords($fileName);
return $fileName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment