43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
trait FileTrait
|
|
{
|
|
public function makeDirectory(string $path)
|
|
{
|
|
if (!is_dir($path)) {
|
|
if (!mkdir($path)) {
|
|
throw new \Exception("디렉토리 생성 실패:{$path}");
|
|
}
|
|
}
|
|
}
|
|
|
|
//디렉토리에 속한 파일 List
|
|
public function getFilesByExtentionType(string $path, string $type = "image"): array
|
|
{
|
|
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;
|
|
}
|
|
// 디렉토리에서 파일 목록 가져오기
|
|
$files = [];
|
|
foreach (scandir($path) as $file) {
|
|
$ext = pathinfo($path . DIRECTORY_SEPARATOR . $file, PATHINFO_EXTENSION);
|
|
// 확장자가 이미지 형식인지 확인
|
|
if (in_array($ext, $exts)) {
|
|
$files[] = $file;
|
|
}
|
|
}
|
|
return $files;
|
|
}
|
|
}
|