Automation/app/Traits/FileTrait.php
2024-09-10 20:12:25 +09:00

48 lines
1.3 KiB
PHP

<?php
namespace App\Traits;
trait FileTrait
{
public function makeDirectory(string $path)
{
if (!is_dir($path)) {
if (!mkdir($path)) {
throw new \Exception("디렉토리 생성 실패:{$path}");
}
}
}
public function isFileType(string $path, string $file, $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;
}
$ext = pathinfo($path . DIRECTORY_SEPARATOR . $file, PATHINFO_EXTENSION);
return in_array($ext, $exts);
}
//디렉토리에 속한 파일 List
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;
}
}