Automation/app/Traits/FileTrait.php
2024-09-14 18:39:00 +09:00

47 lines
1.2 KiB
PHP

<?php
namespace App\Traits;
trait FileTrait
{
final public function makeDirectory(string $path)
{
if (!is_dir($path)) {
if (!mkdir($path)) {
throw new \Exception("디렉토리 생성 실패:{$path}");
}
}
}
final public function isFileType(string $file_ext, $type = "image"): bool
{
switch ($type) {
case "audio":
$exts = ['wav', 'mp3'];
break;
case "video":
$exts = ['mov', 'avi', 'mp4'];
break;
case "image":
default:
$exts = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
break;
}
return in_array($file_ext, $exts);
}
//디렉토리에 속한 파일 List
final public function getFilesByExtentionType(string $path, string $type = "image"): array
{
// 디렉토리에서 파일 목록 가져오기
$files = [];
foreach (scandir($path) as $file) {
// 확장자가 이미지 형식인지 확인
if ($this->isFileType($path, $file, $type)) {
$files[] = $file;
}
}
return $files;
}
}