Last active
December 29, 2024 11:04
-
-
Save okumurakengo/791b1b8dfdb8518db03fc0f0f3548c8a to your computer and use it in GitHub Desktop.
フォルダ内の画像一覧表示
This file contains hidden or 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 | |
| const PAGE_COUNT = 100; | |
| const UPLOADS_DIR = __DIR__ . '/uploads'; | |
| $page = $_GET['page'] ?? 1; | |
| $offset = ($page - 1) * PAGE_COUNT; | |
| $sort_category = $_GET['sort_category'] ?? 'create_date'; | |
| $sort_type = $_GET['sort_type'] ?? 'asc'; | |
| // フォルダが存在するか確認 | |
| if (is_dir(UPLOADS_DIR)) { | |
| // フォルダ内のファイル一覧を取得 | |
| $tmpFiles = scandir(UPLOADS_DIR); | |
| } else { | |
| $tmpFiles = []; | |
| } | |
| $filesArr = []; | |
| foreach ($tmpFiles as $file) { | |
| // 隠しファイル(. や ..)を除外 | |
| if ($file === '.' || $file === '..') { | |
| continue; | |
| } | |
| if ($file === '.gitignore') { | |
| continue; | |
| } | |
| $filePath = UPLOADS_DIR . '/' . $file; | |
| // ファイルが存在しているか確認 | |
| if ( ! is_file($filePath)) { | |
| continue; | |
| } | |
| // 画像の幅と高さを取得 | |
| $dimensions = @getimagesize($filePath); | |
| $width = $dimensions[0] ?? '不明'; | |
| $height = $dimensions[1] ?? '不明'; | |
| $fileCreationDate = date('Y-m-d H:i:s', filemtime($filePath)); | |
| $filesArr[] = [ | |
| 'file' => $file, | |
| 'size' => filesize($filePath), | |
| 'width' => $width, | |
| 'height' => $height, | |
| 'file_creation_date' => $fileCreationDate, | |
| ]; | |
| } | |
| usort($filesArr, function ($a, $b) use ($sort_category, $sort_type) { | |
| if ($sort_category === 'create_date') { | |
| $cmp = strcmp($a['file'], $b['file']); // 作成時刻をファイル名としているので、ファイル名ででソート | |
| } elseif ($sort_category === 'size') { | |
| $cmp = $a['size'] <=> $b['size']; // サイズでソート | |
| } else { | |
| $cmp = 0; // デフォルト | |
| } | |
| return $sort_type === 'desc' ? -$cmp : $cmp; | |
| }); | |
| $total = count($filesArr); | |
| $page_num = ceil($total / PAGE_COUNT); | |
| $limit_count = 0; | |
| $limitArr = []; | |
| foreach ($filesArr as $arr) { | |
| $limit_count++; | |
| if ($offset >= $limit_count) { | |
| continue; | |
| } | |
| if (($offset + PAGE_COUNT) < $limit_count) { | |
| continue; | |
| } | |
| $limitArr[] = $arr; | |
| } | |
| function h($str) { | |
| return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); | |
| } | |
| function formatFileSize($size) { | |
| $res = ''; | |
| if ($size >= 1073741824) { | |
| $res .= round($size / 1073741824, 2) . ' GB / '; | |
| } | |
| if ($size >= 1048576) { | |
| $res .= round($size / 1048576, 2) . ' MB / '; | |
| } | |
| if ($size >= 1024) { | |
| $res .= round($size / 1024, 2) . ' KB / '; | |
| } | |
| return $res . $size . ' バイト'; | |
| } | |
| ?> | |
| <!DOCTYPE html> | |
| <html lang="ja"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <h1>画像一覧</h1> | |
| トータル : <?= $total ?> | |
| <br> | |
| <?php if ($sort_category == 'create_date' && $sort_type == 'asc'): ?> | |
| 作成日昇順 | |
| <?php else: ?> | |
| <a href="./?page=<?= $page ?>&sort_category=create_date&sort_type=asc">作成日昇順</a> | |
| <?php endif; ?> | |
| <?php if ($sort_category == 'create_date' && $sort_type == 'desc'): ?> | |
| 作成日降順 | |
| <?php else: ?> | |
| <a href="./?page=<?= $page ?>&sort_category=create_date&sort_type=desc">作成日降順</a> | |
| <?php endif; ?> | |
| <?php if ($sort_category == 'size' && $sort_type == 'asc'): ?> | |
| サイズ昇順 | |
| <?php else: ?> | |
| <a href="./?page=<?= $page ?>&sort_category=size&sort_type=asc">サイズ昇順</a> | |
| <?php endif; ?> | |
| <?php if ($sort_category == 'size' && $sort_type == 'desc'): ?> | |
| サイズ降順 | |
| <?php else: ?> | |
| <a href="./?page=<?= $page ?>&sort_category=size&sort_type=desc">サイズ降順</a> | |
| <?php endif; ?> | |
| <br> | |
| <?php for ($i = 1; $i <= $page_num; $i++): ?> | |
| <?php if ($i == $page): ?> | |
| <?= $i ?> | |
| <?php else: ?> | |
| <a href="./?page=<?= $i ?>&sort_category=<?= $sort_category ?>&sort_type=<?= $sort_type ?>"><?= $i ?></a> | |
| <?php endif; ?> | |
| <?php endfor; ?> | |
| <br> | |
| <br> | |
| <?php foreach ($limitArr as $arr): ?> | |
| <div> | |
| <?= h($arr['file']); ?> | |
| <br> | |
| 横:<?= $arr['width']; ?>px x 縦:<?= $arr['height']; ?> px | |
| <br> | |
| サイズ: <?= formatFileSize($arr['size']); ?> | |
| <br> | |
| 作成日: <?= $arr['file_creation_date']; ?> | |
| <br> | |
| <img src="./uploads/<?= h($arr['file']) ?>"> | |
| </div> | |
| <hr> | |
| <?php endforeach; ?> | |
| <br> | |
| <?php for ($i = 1; $i <= $page_num; $i++): ?> | |
| <?php if ($i == $page): ?> | |
| <?= $i ?> | |
| <?php else: ?> | |
| <a href="./?page=<?= $i ?>"><?= $i ?></a> | |
| <?php endif; ?> | |
| <?php endfor; ?> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment