getMimeType(ltrim($ext, '.')); } /** * Try to guess the mimetype from the filename. * * Return null if it did not manage to guess it. */ public function guessMimeTypeFromFilename(string $filename): ?string { $ext = pathinfo($filename, PATHINFO_EXTENSION); if (empty($ext)) { return null; } return self::getMimeTypeFromExt($ext); } /** * Test si le fichier passé en paramètre est une image. */ public function isImage(File $file): bool { $mimetype = $file->getMimeType() ?: $this->guessMimeTypeFromFilename($file->getName()); return boolval(preg_match('#^image#', $mimetype)); } /** * Génère un nom de fichier temporaire situé dans le répertoire var/tmp, * avec l'extension et le préfixe donnés. * * @param string $ext * @param string $prefix * @return string * @throws RuntimeException */ public function getTempFilename(string $ext = 'tmp', string $prefix = ''): string { if (empty($ext)) { throw new RuntimeException('Extension can not be empty'); } $tempDir = Path::getProjectDir() . '/var/tmp'; if (!is_dir($tempDir)) { mkdir($tempDir); } return $tempDir . '/' . $prefix . uniqid() . '.' . $ext; } }