. */ namespace SP\Util; use FilesystemIterator; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use SP\Core\Exceptions\FileNotFoundException; use SP\DataModel\FileData; /** * Class FileUtil * * @package SP\Util */ final class FileUtil { const IMAGE_MIME = ['image/jpeg', 'image/png', 'image/bmp', 'image/gif']; /** * Removes a directory in a recursive way * * @param $dir * * @return bool * @throws FileNotFoundException * @see https://stackoverflow.com/a/7288067 */ public static function rmdir_recursive($dir): bool { if (!is_dir($dir)) { throw new FileNotFoundException('Directory does not exist'); } $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST ); foreach ($it as $file) { if ($file->isDir()) { rmdir($file->getPathname()); } else { unlink($file->getPathname()); } } return rmdir($dir); } /** * @param FileData $fileData * * @return bool */ public static function isImage(FileData $fileData): bool { return in_array(strtolower($fileData->getType()), self::IMAGE_MIME, true); } }