-
-
Save iMaz1n/84259d696ac3c67615b547bab0f5b198 to your computer and use it in GitHub Desktop.
PHP List images in folder
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 | |
/* | |
list all the files with extension nn on a folder | |
the first parameter of glob is a regex to match files. | |
you can do: | |
path_to_dir/*.html | |
to select all the .html files in the path_to_dir folder | |
*NOTE:* the regex on glob is case sensitive | |
*/ | |
$images = glob("{*.jpg,*.jpeg,*.png,*.gif,*.JPG,*.JPEG,*.PNG,*.GIF}", GLOB_BRACE); | |
/** | |
* Converts bytes into human readable file size. | |
* | |
* @param string $bytes | |
* @return string human readable file size (2,87 Мб) | |
* @author Mogilev Arseny | |
*/ | |
function fileSizeConvert($bytes){ | |
$bytes = floatval($bytes); | |
$arBytes = array( | |
0 => array( | |
"UNIT" => "TB", | |
"VALUE" => pow(1024, 4)), | |
1 => array( | |
"UNIT" => "GB", | |
"VALUE" => pow(1024, 3)), | |
2 => array( | |
"UNIT" => "MB", | |
"VALUE" => pow(1024, 2)), | |
3 => array( | |
"UNIT" => "KB", | |
"VALUE" => 1024), | |
4 => array( | |
"UNIT" => "B", | |
"VALUE" => 1), | |
); | |
foreach($arBytes as $arItem){ | |
if($bytes >= $arItem["VALUE"]){ | |
$result = $bytes / $arItem["VALUE"]; | |
$result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"]; | |
break; | |
} | |
} | |
return $result; | |
} | |
?><!doctype html> | |
<html lang="en"> | |
<head> | |
<!--<meta charset="utf-8"> --> | |
<title> File list </title> | |
</head> | |
<body style="font-family: monospace, sans-serif;"> | |
<ul> | |
<?php foreach ($images as $img): ?> | |
<li><?php echo filemtime($img).' – <a href="'.$img.'">'.$img."</a> – ". fileSizeConvert(filesize($img)) ."<br>\n"; ?></li> | |
<?php endforeach ?> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment