Skip to content

Instantly share code, notes, and snippets.

@baamenabar
Last active May 3, 2020 06:29
Show Gist options
  • Save baamenabar/4023919 to your computer and use it in GitHub Desktop.
Save baamenabar/4023919 to your computer and use it in GitHub Desktop.
PHP List images in folder
<?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).' &ndash; <a href="'.$img.'">'.$img."</a> &ndash; ". 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