Created
December 17, 2014 14:48
-
-
Save xkeshav/6c46dc0b80f1f58833c9 to your computer and use it in GitHub Desktop.
want to shorten the code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$loc = '/opt/lampp/archive/data/'; | |
$i = 0; | |
latestThumb($loc); | |
function pre($what, $stop = false ){ | |
echo "<pre>"; | |
print_r($what); | |
echo "</pre>"; | |
if ($stop) die; | |
} | |
function latestThumb($loc) | |
{ | |
$RealPath = realpath($loc); | |
// var_dump($RealPath); | |
$flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::NEW_CURRENT_AND_KEY | FilesystemIterator::KEY_AS_FILENAME; | |
$Directory = new RecursiveDirectoryIterator($RealPath, $flags); | |
// echo "<pre>"; print_r($Directory); echo "</pre>"; | |
//pre($Directory); | |
$Dir_ = iterator_to_array($Directory); | |
// $output = 'ok'; | |
//first level | |
foreach ($Directory as $dir) { | |
$currentDirectory = $Directory->current(); | |
// var_dump($currentDirectory); | |
// 'loop': | |
if($currentDirectory->isDir()) { | |
// echo "_here"; | |
$currentPath = $currentDirectory->getRealPath(); | |
// pre($currentPath); | |
// echo __LINE__; | |
$output = findLatestThumb($currentDirectory); | |
var_dump($output); | |
die; | |
// pre($output); | |
// goto loop; | |
} | |
} | |
// var_dump($return); | |
// exit; | |
// // pre($Dir_); | |
// ksort($Dir_); // namewise ASC sort | |
// pre($Dir_); | |
// $Dir_latest = array_pop($Dir_); | |
// pre($Dir_latest); | |
// if($Dir_latest->isDir()) { | |
// $folderName = $Dir_latest->getFileName(); | |
// $realpath = $Dir_latest->getRealPath(); | |
// var_dump($folderName); | |
// var_dump($realpath); | |
// if($folderName == 'thumbnails') { | |
// $folderName = $Dir_latest->getFileName(); | |
// $realpath = $Dir_latest->getRealPath(); | |
// echo "inside thumbnail"; | |
// $th_rp = new RecursiveDirectoryIterator($realpath,$flags); | |
// $th_rp_ = iterator_to_array($th_rp); | |
// var_dump($th_rp_); | |
// ksort($th_rp_); | |
// var_dump($th_rp_); | |
// foreach($th_rp_ as $thumb) { | |
// var_dump(substr($thumb->getFileName(), 10)); | |
// } | |
// $th_rp_latest = array_pop($th_rp_); | |
// $th_folderName = $th_rp_latest->getFileName(); | |
// $th_realpath = $th_rp_latest->getRealPath(); | |
// var_dump($th_realpath,$th_folderName); | |
// exit; | |
// //return $realpath; | |
// } else { | |
// echo "<br/>Again"; | |
// latestThumb($realpath); | |
// } | |
// } | |
// // echo | |
// die; | |
// //alternative : sort and get first using current | |
// krsort($Dir_); | |
// pre($Dir_,1); | |
// sort to get recent folder first | |
// var_dump($Dir_); | |
// exit | |
// label assigned. if it is directory than select first latest entry of directory and return the new path | |
// foreach ($Dir_ as $dir) { | |
// $first = $dir; | |
// // print_r($first); | |
// $isDirectory = $first->isDir(); | |
// $isDirectory = $first->isDir(); | |
// $folderName = $first->getFileName(); | |
// $realpath = $first->getRealPath(); | |
// var_dump($folderName); | |
// // if($folderName == 'thumbnails') { | |
// // $di = new DirectoryIterator($realpath); | |
// // var_dump($di); | |
// // continue; | |
// // } | |
// if($isDirectory) { | |
// $fullpath = $first->getPathName(); | |
// $realpath = $first->getRealPath(); | |
// $pathInfo = $first->getPathInfo(); | |
// // echo __LINE__."<pre>"; print_r($pathInfo->getRealPath()); | |
// var_dump($realpath); | |
// if($folderName == 'thumbnails') { | |
// echo "<h3>thumbnail folder comes</h3>"; | |
// $di = new DirectoryIterator($fullpath); | |
// var_dump($di); | |
// break; | |
// } else { | |
// echo "Inside else"; | |
// latestThumb($realpath); | |
// } | |
// } | |
// } | |
} | |
function findLatestThumb($path){ | |
// var_dump($path); | |
// var_dump($path->isDir()); | |
$realpath_ = ''; | |
$RealPath = $path->getRealPath(); | |
//var_dump($RealPath); | |
$flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::NEW_CURRENT_AND_KEY | FilesystemIterator::KEY_AS_FILENAME; | |
$Directory = new RecursiveDirectoryIterator($RealPath, $flags); | |
$Dir_ = iterator_to_array($Directory); | |
ksort($Dir_); // namewise ASC sort | |
//pre($Dir_); //die; | |
$Dir_latest = array_pop($Dir_); | |
$folderName = $Dir_latest->getFileName(); | |
$realpath = $Dir_latest->getRealPath(); | |
if($folderName !== 'thumbnails') { | |
// echo "<br/>"."inside if"; | |
findLatestThumb($Dir_latest); | |
} | |
else { | |
echo "got the thumbnails"; | |
// return "jaimatadi"; | |
//findLatestThumb($Dir_latest); | |
$RealPath = $Dir_latest->getRealPath(); | |
// var_dump($RealPath); | |
$flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::NEW_CURRENT_AND_KEY | FilesystemIterator::KEY_AS_FILENAME; | |
$Directory = new RecursiveDirectoryIterator($RealPath, $flags); | |
$Dir_ = iterator_to_array($Directory); | |
// var_dump($Dir_); | |
ksort($Dir_); // namewise ASC sort | |
// pre($Dir_); //die; | |
$Dir_latest = array_pop($Dir_); | |
$folderName = $Dir_latest->getFileName(); | |
$realpath_ = 'Trilok'; | |
// $realpath_ = $Dir_latest->getRealPath(); | |
// echo __LINE__;var_dump($realpath_); | |
// pre($realpath); | |
// break; | |
// exit; | |
//$realpath; | |
} | |
//var_dump($realpath_); | |
// echo "hhh"; | |
// die; | |
return $realpath_; | |
//return $realpath_; | |
} | |
// // print_r($Directory->current()); | |
// if( $Directory->hasChildren() ) { | |
// $ch = $Directory->getChildren(); | |
// echo __LINE__; | |
// var_dump($ch->key()); | |
// $fsi = new RecursiveIteratorIterator($ch, $flags); | |
// $fsi_ = iterator_to_array($fsi, true); | |
// // var_dump($fsi_); | |
// krsort($fsi_); | |
// var_dump($fsi_); | |
// exit; | |
// foreach ($fsi_ as $f) { | |
// var_dump($f->getFileName()); | |
// if($f->isDir()) { | |
// $fi = new RecursiveDirectoryIterator($f, FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_FILEINFO); | |
// $It = new RecursiveIteratorIterator($fi); | |
// $Regex = new RegexIterator($It, '#/thumbnails/.*#i', RecursiveRegexIterator::GET_MATCH); | |
// $th = iterator_to_array($Regex); | |
// krsort($th); | |
// var_dump($th); | |
// foreach($th as $v){ | |
// var_dump($v); | |
// } | |
// // var_dump(__LINE__,iterator_to_array($fi)); | |
// } | |
// } | |
// } | |
// } | |
// exit; | |
// $files_ = iterator_to_array($Directory, true); | |
// var_dump($files_); | |
// // exit; | |
// exit; | |
// foreach ($files_ as $file) { | |
// $fsi = new RecursiveDirectoryIterator($file, | |
// FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ); | |
// $fsi_ = iterator_to_array($fsi, true); | |
// krsort($fsi_); | |
// var_dump($fsi_); | |
// foreach ($fsi_ as $f) { | |
// if($f->isDir()){ | |
// // $d = scandir($f->getPathname(),1); | |
// // var_dump($d); | |
// $th = new RecursiveDirectoryIterator($f->getPathname().DS.'thumbnails', FilesystemIterator::SKIP_DOTS | FilesystemIterator::CURRENT_AS_FILEINFO); | |
// var_dump($th); | |
// // $fd_ = iterator_to_array($fd,true); | |
// // krsort($fd_); | |
// // var_dump($fd_); | |
// $format = "<br/> Directory : %s , Created at : %s , Modified at : %s<hr/>"; | |
// printf($format, $f->getPathname(), $f->getCtime(), $f->getMtime()); | |
// } | |
// } | |
// } | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment