public static function getSafeFileName($fileName, $directory = null) {
        $lastDot = strrpos($fileName, '.');
        $file = $lastDot ? substr($fileName, 0, $lastDot) : $fileName;
        $ending = $lastDot ? substr($fileName, $lastDot+1) : '';
        $safeName = self::removeSpecChars($file);
        $safeEnding = empty($ending)
            ? ''
            : '.'.self::removeSpecChars($ending);
        
        if ($directory != null && file_exists($directory.$safeName.$safeEnding)) {
            $count = 1;
            while (file_exists($directory.$safeName.'_'.$count.$safeEnding)) {
                $count++;
            }
            $safeName = $safeName.'_'.$count;
        } 
        
        return $safeName.$safeEnding;
    }
public static function getSafeFileName($fileName, $directory = null) {
        $lastDot = strrpos($fileName, '.');
        $file = $lastDot ? substr($fileName, 0, $lastDot) : $fileName;
        $ending = $lastDot ? substr($fileName, $lastDot+1) : '';
        $safeName = self::removeSpecChars($file);
        $safeEnding = empty($ending)
            ? ''
            : '.'.self::removeSpecChars($ending);
        
        if ($directory != null && file_exists($directory.$safeName.$safeEnding)) {
            $count = 1;
            while (file_exists($directory.$safeName.'_'.$count.$safeEnding)) {
                $count++;
            }
            $safeName = $safeName.'_'.$count;
        } 
        
        return $safeName.$safeEnding;
    }
public static function getSafeFileName ( $ fileName , $ directory = null ) { $ lastDot = strrpos ( $ fileName , '.' ) ; $ file = $ lastDot ? substr ( $ fileName , 0 , $ lastDot ) : $ fileName ; $ ending = $ lastDot ? substr ( $ fileName , $ lastDot + 1 ) : '' ; $ safeName = self :: removeSpecChars ( $ file ) ; $ safeEnding = empty ( $ ending ) ? '' : '.' . self :: removeSpecChars ( $ ending ) ; if ( $ directory != null && file_exists ( $ directory . $ safeName . $ safeEnding ) ) { $ count = 1 ; while ( file_exists ( $ directory . $ safeName . '_' . $ count . $ safeEnding ) ) { $ count ++ ; } $ safeName = $ safeName . '_' . $ count ; } return $ safeName . $ safeEnding ; }
Get a safe filename for a proposed filename.

If directory is specified it will return a filename which is
safe to not overwritte an existing file. This function is not injective.

@param string $fileName
@param string $directory

@return string
Get a safe filename for a proposed filename .