face-recognition.js/public_html/pictures/index.php

58 lines
1.2 KiB
PHP
Executable File

<?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) );