protected function validateFilename($filename, $path)
    {
        $fileParts = explode('.', $filename);
        if (count($fileParts) > 1) {
            $extension = end($fileParts);
            array_pop($fileParts);
            $fileNameWithoutExtension = implode('-', $fileParts);
            $fileNameWithoutExtension = StringUtil::slugify($fileNameWithoutExtension);
            $filename = $fileNameWithoutExtension . '.' . $extension;
        } else {
            $filename = StringUtil::slugify($filename);
        }

        if (file_exists($path . '/' . $filename)) {
            $fileParts = explode('.', $filename);
            if (count($fileParts) > 1) {
                $extension = end($fileParts);
                array_pop($fileParts);
                $fileNameWithoutExtension = implode('-', $fileParts);
                $fileNameWithoutExtension .= '-copy';
                $filename = $fileNameWithoutExtension . '.' . $extension;
            } else {
                $filename .= '-copy';
            }

            return $this->validateFilename($filename, $path);
        }

        return $filename;
    }
protected function validateFilename($filename, $path)
    {
        $fileParts = explode('.', $filename);
        if (count($fileParts) > 1) {
            $extension = end($fileParts);
            array_pop($fileParts);
            $fileNameWithoutExtension = implode('-', $fileParts);
            $fileNameWithoutExtension = StringUtil::slugify($fileNameWithoutExtension);
            $filename = $fileNameWithoutExtension . '.' . $extension;
        } else {
            $filename = StringUtil::slugify($filename);
        }

        if (file_exists($path . '/' . $filename)) {
            $fileParts = explode('.', $filename);
            if (count($fileParts) > 1) {
                $extension = end($fileParts);
                array_pop($fileParts);
                $fileNameWithoutExtension = implode('-', $fileParts);
                $fileNameWithoutExtension .= '-copy';
                $filename = $fileNameWithoutExtension . '.' . $extension;
            } else {
                $filename .= '-copy';
            }

            return $this->validateFilename($filename, $path);
        }

        return $filename;
    }
protected function validateFilename ( $ filename , $ path ) { $ fileParts = explode ( '.' , $ filename ) ; if ( count ( $ fileParts ) > 1 ) { $ extension = end ( $ fileParts ) ; array_pop ( $ fileParts ) ; $ fileNameWithoutExtension = implode ( '-' , $ fileParts ) ; $ fileNameWithoutExtension = StringUtil :: slugify ( $ fileNameWithoutExtension ) ; $ filename = $ fileNameWithoutExtension . '.' . $ extension ; } else { $ filename = StringUtil :: slugify ( $ filename ) ; } if ( file_exists ( $ path . '/' . $ filename ) ) { $ fileParts = explode ( '.' , $ filename ) ; if ( count ( $ fileParts ) > 1 ) { $ extension = end ( $ fileParts ) ; array_pop ( $ fileParts ) ; $ fileNameWithoutExtension = implode ( '-' , $ fileParts ) ; $ fileNameWithoutExtension .= '-copy' ; $ filename = $ fileNameWithoutExtension . '.' . $ extension ; } else { $ filename .= '-copy' ; } return $ this -> validateFilename ( $ filename , $ path ) ; } return $ filename ; }
Converts filename to lowercase, remove non-ascii chars
And adds "-copy" if the file already exists

@param $filename
@param $path

@return string
Converts filename to lowercase remove non - ascii chars And adds - copy if the file already exists