Gestion du listing des pictures [public_html/pictures/index.php]
After Width: | Height: | Size: 172 KiB |
After Width: | Height: | Size: 558 KiB |
After Width: | Height: | Size: 184 KiB |
After Width: | Height: | Size: 167 KiB |
After Width: | Height: | Size: 6.0 KiB |
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
|
||||
/* [0] Initialization
|
||||
=========================================================*/
|
||||
/* (1) Catch 'pictures' dir */
|
||||
$dir = dirname(__FILE__);
|
||||
|
||||
/* (2) Defining authorized extensions */
|
||||
$ext = ['jpg', 'png', 'jpeg'];
|
||||
$checker = '/\.('. implode('|', $ext) .')$/i';
|
||||
|
||||
|
||||
/* [2] Listing all pictures recursivly
|
||||
=========================================================*/
|
||||
function getImages($directory){
|
||||
|
||||
/* (1) We get the direct content of the dir */
|
||||
$files = scandir($directory);
|
||||
$fetched = [];
|
||||
|
||||
// if error
|
||||
if( !is_array($files) ) return [];
|
||||
|
||||
|
||||
/* (2) For each element in directory */
|
||||
foreach( $files as $e=>$fname ){
|
||||
|
||||
// {0} Avoid . and .. //
|
||||
if( in_array($fname, ['.', '..']) )
|
||||
continue;
|
||||
|
||||
|
||||
$element = "$directory/$fname";
|
||||
|
||||
// {1} If subdir (excepted . and ..)-> applying recursivly //
|
||||
if( is_dir($element) )
|
||||
$fetched[$fname] = getImages($element);
|
||||
|
||||
// {2} If not picture -> unset //
|
||||
else if( !preg_match($checker, $fname) )
|
||||
$fetched[] = $fname;
|
||||
|
||||
}
|
||||
|
||||
/* (3) Returning result */
|
||||
return $fetched;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [2] On affiche le résultat
|
||||
=========================================================*/
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode( getImages($dir) );
|
After Width: | Height: | Size: 282 KiB |
After Width: | Height: | Size: 8.0 KiB |