Skip to content

Instantly share code, notes, and snippets.

@xkeshav
Created December 18, 2014 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkeshav/7a9484dffecb35f252fb to your computer and use it in GitHub Desktop.
Save xkeshav/7a9484dffecb35f252fb to your computer and use it in GitHub Desktop.
Iterator file which will retrive latest image of latest folder distributed in timetamp nomeniclature of without scanning complete directory
<?php
// error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ALL ^ E_NOTICE);
class latest {
private $basepath;
private $flags;
public $parentDir;
private $thumbDirName = 'thumbnails';
public $i = 1;
public $latestThumb = [];
function __construct($base = __DIR__) {
// var_dump($base);
$this->basepath = $base;
$this->flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::NEW_CURRENT_AND_KEY | FilesystemIterator::KEY_AS_FILENAME;
}
public static function pre(){
// echo "here";
$num = func_num_args();
// var_dump($num);
if($num >0 ) {
$farg = func_get_arg(0);
if(is_array($farg)) {
$num = count($farg);
// var_dump($num,!$num);
do {
echo "<pre>";
print_r($farg);
echo "</pre>";
} while(false);
} else {
var_dump($farg);
}
}
}
public function latestThumb()
{
//die;
$dir = (func_num_args()) ? func_get_arg(0) : $this->basepath;
$RealPath = realpath($dir);
// var_dump($RealPath);
$Directory = new RecursiveDirectoryIterator($RealPath, $this->flags);
// echo "<pre>"; print_r($Directory); echo "</pre>";
// self::pre($Directory);
$Dir_ = iterator_to_array($Directory);
ksort($Dir_);
// self::pre($Dir_);
//first level
// foreach ($Directory as $kDir=>$vDir) {
foreach ($Dir_ as $kDir=>$vDir) {
// var_dump($kDir,$vDir);
// var_dump($vDir->isDir());
// var_dump($vDir->current()->isDir()); // FATAL ERROR
// if($Directory->current()->isDir()) {
if($vDir->isDir()) {
$this->findLatest($vDir);
continue;
}
}
var_dump($this->latestThumb);
}
public function findLatest($dir){
// var_dump($dir);
$currentDirName = $dir->getFileName();
$currentDirPath = $dir->getRealPath();
var_dump($currentDirName,$currentDirPath);
$Directory = new RecursiveDirectoryIterator($currentDirPath, $this->flags);
$Dir_ = iterator_to_array($Directory);
/* Sort ASC so that latest created folder comes as last entry of array
folder are genarted dynamically in below format
<foldername><num><YYYY><MM><DD><HH><MM><SS>_thumb.jpg
*/
// echo "<hr/>";
// self::pre($Dir_);
ksort($Dir_);
// self::pre($Dir_);
// echo "<hr/><hr/>";
// get the last entry of sorted array which will be recent folder
$key = array_key_exists("thumbnails", $Dir_);
// var_dump($key);
if($key) {
echo "Towards the thumbnails";
var_dump($Dir_["thumbnails"]);
$this->findLatest($Dir_["thumbnails"]);
return;
// var_dump($tkey);
}
$dir_latest = array_pop($Dir_);
/* check whether it is file or Directory */
// var_dump("Parent:".$currentDirName);
// self::pre($currentDirName);
// echo __LINE__;
if( $currentDirName == "thumbnails") {
// echo "<br/>YES . only thumbnails";
$thumbFolder = true;
$t_name = $dir_latest->getBaseName();
$t_path = $dir_latest->getRealPath();
$isFile = $dir_latest->isFile();
$isReadable = $dir_latest->isReadable();
// var_dump($isFile , $isReadable);
if($isFile && $isReadable) {
$this->latestThumb[] = [$t_name,$t_path];
}
// var_dump($this->latestThumb);
return ;
} else {
// echo "Inside other else";
// var_dump($dir_latest->getRealPath());
// var_dump($dir_latest->getBaseName());
// var_dump($dir_latest->getFileName());
// var_dump($dir_latest);
$this->findLatest($dir_latest);
return;
}
}
}
$loc = '/opt/lampp/archive/data/';
$obj = new latest($loc);
$obj->latestThumb();
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment