protected function suffixFileName(string $fileName): string
    {
        $pathInfo = pathinfo($fileName);
        $dirname = $pathInfo['dirname'].DIRECTORY_SEPARATOR;
        $filename = $pathInfo['filename'];
        $extension  = empty($pathInfo['extension']) ? '' : '.'.$pathInfo['extension'];

        $i = 0;
        do {
            $i++;

            if ($i > $this->maxDuplicateNumber) {
                $duplicateExtension = uniqid();
            } else {
                $duplicateExtension = $i;
            }

            $resultName = $dirname.$filename."_$duplicateExtension".$extension;
        } while (file_exists($resultName));

        return $resultName;
    }
protected function suffixFileName(string $fileName): string
    {
        $pathInfo = pathinfo($fileName);
        $dirname = $pathInfo['dirname'].DIRECTORY_SEPARATOR;
        $filename = $pathInfo['filename'];
        $extension  = empty($pathInfo['extension']) ? '' : '.'.$pathInfo['extension'];

        $i = 0;
        do {
            $i++;

            if ($i > $this->maxDuplicateNumber) {
                $duplicateExtension = uniqid();
            } else {
                $duplicateExtension = $i;
            }

            $resultName = $dirname.$filename."_$duplicateExtension".$extension;
        } while (file_exists($resultName));

        return $resultName;
    }
protected function suffixFileName ( string $ fileName ) : string { $ pathInfo = pathinfo ( $ fileName ) ; $ dirname = $ pathInfo [ 'dirname' ] . DIRECTORY_SEPARATOR ; $ filename = $ pathInfo [ 'filename' ] ; $ extension = empty ( $ pathInfo [ 'extension' ] ) ? '' : '.' . $ pathInfo [ 'extension' ] ; $ i = 0 ; do { $ i ++ ; if ( $ i > $ this -> maxDuplicateNumber ) { $ duplicateExtension = uniqid ( ) ; } else { $ duplicateExtension = $ i ; } $ resultName = $ dirname . $ filename . "_$duplicateExtension" . $ extension ; } while ( file_exists ( $ resultName ) ) ; return $ resultName ; }
Rename a file if it already exists at its destination.
Renaming is done by adding a duplicate number to the file name. E.g. existingFileName_1.ext.
After a max duplicate number, renaming the file will switch over to generating a random suffix.

@param string $fileName  Complete path to the file.
@return string           The suffixed file name.
Rename a file if it already exists at its destination . Renaming is done by adding a duplicate number to the file name . E . g . existingFileName_1 . ext . After a max duplicate number renaming the file will switch over to generating a random suffix .