Advertisement
Clowerweb

Generate Unique Filename

Nov 9th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.45 KB | None | 0 0
  1. public static function uniqueFilename(array $file) : string {
  2.     if(is_array($file) and $file['name'] !== '') {
  3.         $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
  4.         $file_name = uniqid() . '.' . $extension;
  5.  
  6.         if(file_exists('/path/to/file' . $file_name)) {
  7.             static::uniqueFileName($file);
  8.             return '';
  9.         }
  10.  
  11.         return $file_name;
  12.     } else {
  13.         throw new \Exception("Invalid file.");
  14.     }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement