Converts filename to lowercase, remove non-ascii chars
And adds "-copy" if the file already exists

@param $filename
@param $path

@return string
183471
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;
    }
537282
static function changeName($path, $name)
    {
        $f = self::fileInfo($path);
        return $f['dir'].$name.'.'.$f['ext'];
    }
35821
protected function getTitleFromFilename(string $filename)
    {
        $withoutExtension = pathinfo($filename, PATHINFO_FILENAME);
        return $withoutExtension !== '' ? $withoutExtension : $filename;
    }
266572
public static function getFileName($filePath) {
        $lastIndex = strripos($filePath, DIRECTORY_SEPARATOR);
        $fileName = substr($filePath, $lastIndex + 1);
        return substr($fileName, 0, strripos($fileName, "."));
    }
373627
static function _NormalizeFilename($filename){
		$_filename = "";
		while(1){
			$_filename = preg_replace('/[^\/]+\/+\.\.\//','/',$filename); // "/path/to/dir/../tmp/images/" -> "/path/to/tmp/images/"
			if($_filename==$filename){ break; }
			$filename = $_filename;
		}
		$filename = $_filename;

		$filename = preg_replace('/\/\/+/','/',$filename); // "/path/to//tmp/dir//" -> "/path/to/tmp/dir/"

		return $filename;
	}
148063
public static function normalizeFileName($fileName, $withExtension = true)
    {
        // Remove filename's extension.
        $fileName = basename($fileName);
        $extension = substr($fileName, strrpos($fileName, ".") + 1, strlen($fileName) - strrpos($fileName, "."));
        $fileName = substr($fileName, 0, strrpos($fileName, "."));

        // Convert to lowercase.
        $fileName = strtolower($fileName);

        // Convert characters to the range a-z_-
        $fileName = strtr($fileName, "√°√©√≠√≥√∫√†√®√¨√≤√π√§√´√Ø√∂√º√¢√™√Æ√¥√ª√±√ß", "aeiouaeiouaeiouaeiounc");
        $fileName = preg_replace("/([\W]+)/", "_", $fileName);

        if ($withExtension) {
            $fileName .= '.' . $extension;
        }

        return $fileName;
    }
1133713
public static function filenameToType($filename)
    {
        // In case the path is a URL, strip any query string before getting extension
        $qpos = strpos($filename, '?');
        if ($qpos !== false) {
            $filename = substr($filename, 0, $qpos);
        }
        $pathinfo = self::mb_pathinfo($filename);
        return self::_mime_types($pathinfo['extension']);
    }
341031
public function cssVersion($fileName)
    {
        $filePath = sprintf("%s/../web%s", $this->kernelRootDir, $fileName);

        if (true === file_exists($filePath)) {
            return sprintf("%s?v=%s", $fileName, substr(md5_file($filePath), 0, 5));
        }

        return sprintf("%s?v=%s", $fileName, substr(sha1(microtime()), 0, 6));
    }
414641
public function save($path, $filename)
    {
        if (!file_exists($path)) {
            $this->makeDirectory($path);
        }

        return file_put_contents($path . '/' . $filename, $this->getContents());
    }
719468
protected static function trimRootPath( $fileName )
	{
		$rootPath	= isset( $_SERVER['DOCUMENT_ROOT'] ) ? $_SERVER['DOCUMENT_ROOT'] : "";
		if( !$rootPath || !$fileName )
			return;
		$fileName	= str_replace( '\\', "/", $fileName );
		$cut		= substr( $fileName, 0, strlen( $rootPath ) );
		if( $cut == $rootPath )
			$fileName	= substr( $fileName, strlen( $rootPath ) );
		return $fileName;
	}
664897
public function getFileName($path, $filename)
	{
		if (file_exists("$path/$filename")) {
			$filename = Nette\Utils\Random::generate() . '_' . $filename;
			$filename = $this->getFileName($path, $filename);
		} else {
			$filename;
		}

		return $filename;
	}
965814
public static function uniqueFilename($filename, $path) {
	
		// fixes path if not containing trailing slash
		self::fixTrailingSlash($path);
	
		if (file_exists($path . $filename)) {
			$dot  = strrpos($filename,'.');
			$ext  = substr($filename,$dot);
			$name = substr($filename,0,$dot);
			$hash = substr(md5(time()),0,6);
			$filename = $name . '-' . $hash . $ext;
		}
	
		return $filename;
	
	}
370792
protected function logFilename($filename, $filePath)
    {
        $filePath = trim(rtrim($filePath, "\/"));
        $filename = trim(trim($filename, "\/"));

        $path = $filePath . DIRECTORY_SEPARATOR . $filename;

        if (!realpath($path) || !file_exists($path)) {
            touch($path);
        }

        return realpath($path);
    }
1348863
protected function mangle_pathname($localname) {
        if ($this->encoding === 'utf-8') {
            return $localname;
        }

        $converted = core_text::convert($localname, 'utf-8', $this->encoding);
        $original  = core_text::convert($converted, $this->encoding, 'utf-8');

        if ($original === $localname) {
            $result = $converted;

        } else {
            // try ascii conversion
            $converted2 = core_text::specialtoascii($localname);
            $converted2 = core_text::convert($converted2, 'utf-8', $this->encoding);
            $original2  = core_text::convert($converted, $this->encoding, 'utf-8');

            if ($original2 === $localname) {
                //this looks much better
                $result = $converted2;
            } else {
                //bad luck - the file name may not be usable at all
                $result = $converted;
            }
        }

        $result = preg_replace('/\.\.+/', '', $result);
        $result = ltrim($result); // no leading /

        if ($result === '.') {
            $result = '';
        }

        return $result;
    }
218476
public static function findInPath($filename, $path)
    {
        $asset_path = DirectoryHelper::slashDirname($path) . $filename;
        if (file_exists($asset_path)) {
            return self::buildWebPath($asset_path);
        }
        return null;
    }
702948
protected function cleanFilename($filename)
    {
        /** @var FileIDHelper $helper */
        $helper = Injector::inst()->get(HashFileIDHelper::class);
        return $helper->cleanFilename($filename);
    }
901950
private function removeExtension($filename)
    {
        $pos = mb_strrpos($filename, '.');
        if ($pos === false) {
            return $filename;
        }

        return mb_substr($filename, 0, $pos);
    }