public function createUniqueFileName($file)
    {
        $ext = '.'.$file->guessExtension();
        $basename = trim(str_replace('.'.$file->getClientOriginalExtension(), '', $file->getClientOriginalName()));
        $basename = str_replace(' ', '-', $basename);
        $basename = strtolower($basename);

        $existing = $this->filesystem->listKeys($basename);
        if (isset($existing['keys'])) {
            $existing = $existing['keys'];
        }

        if (count($existing)) {
            $ids = [1];
            foreach ($existing as $neighbor) {
                $neighbor = str_replace($ext, '', $neighbor);
                if (preg_match('/(\d+)$/', $neighbor, $matches)) {
                    $ids[] = intval($matches[1]);
                }
            }

            rsort($ids);
            $id = reset($ids);
            ++$id;

            $basename = $basename.'-'.$id;
        }

        return $basename.$ext;
    }
public function createUniqueFileName($file)
    {
        $ext = '.'.$file->guessExtension();
        $basename = trim(str_replace('.'.$file->getClientOriginalExtension(), '', $file->getClientOriginalName()));
        $basename = str_replace(' ', '-', $basename);
        $basename = strtolower($basename);

        $existing = $this->filesystem->listKeys($basename);
        if (isset($existing['keys'])) {
            $existing = $existing['keys'];
        }

        if (count($existing)) {
            $ids = [1];
            foreach ($existing as $neighbor) {
                $neighbor = str_replace($ext, '', $neighbor);
                if (preg_match('/(\d+)$/', $neighbor, $matches)) {
                    $ids[] = intval($matches[1]);
                }
            }

            rsort($ids);
            $id = reset($ids);
            ++$id;

            $basename = $basename.'-'.$id;
        }

        return $basename.$ext;
    }
public function createUniqueFileName ( $ file ) { $ ext = '.' . $ file -> guessExtension ( ) ; $ basename = trim ( str_replace ( '.' . $ file -> getClientOriginalExtension ( ) , '' , $ file -> getClientOriginalName ( ) ) ) ; $ basename = str_replace ( ' ' , '-' , $ basename ) ; $ basename = strtolower ( $ basename ) ; $ existing = $ this -> filesystem -> listKeys ( $ basename ) ; if ( isset ( $ existing [ 'keys' ] ) ) { $ existing = $ existing [ 'keys' ] ; } if ( count ( $ existing ) ) { $ ids = [ 1 ] ; foreach ( $ existing as $ neighbor ) { $ neighbor = str_replace ( $ ext , '' , $ neighbor ) ; if ( preg_match ( '/(\d+)$/' , $ neighbor , $ matches ) ) { $ ids [ ] = intval ( $ matches [ 1 ] ) ; } } rsort ( $ ids ) ; $ id = reset ( $ ids ) ; ++ $ id ; $ basename = $ basename . '-' . $ id ; } return $ basename . $ ext ; }
Create a unique filename based on the original filename.

This checks the filesystem for other files that start with the original base name.
It does not check the database to avoid overwriting files that are not persisted
to the database for whatever reason.

@param \Symfony\Component\HttpFoundation\File\UploadedFile $file

@return string
Create a unique filename based on the original filename .