PHP Case Insensitive Version of file_exists()

后端 未结 14 1463
忘掉有多难
忘掉有多难 2020-11-30 07:47

I\'m trying to think of the fastest way to implement a case insensitive file_exists function in PHP. Is my best bet to enumerate the file in the directory and do a strtolowe

14条回答
  •  一整个雨季
    2020-11-30 08:17

    I have improved John Himmelman's function and come up with this:
    suppose that i have a catch system \iMVC\kernel\caching\fileCache

    function resolve_path($path)
    {
        # check if string is valid
        if(!strlen($path)) return FALSE;
        # a primary check
        if(file_exists($path)) return $path;
        # create a cache signiture
        $cache_sig = __METHOD__."@$path";
        # open the cache file
        $fc = new \iMVC\kernel\caching\fileCache(__CLASS__);
        # check cache file and validate it
        if($fc->isCached($cache_sig) && file_exists($fc->retrieve($cache_sig)))
        {
            # it was a HIT!
            return $fc->retrieve($cache_sig);
        }
        # if it is ab
        $is_absolute_path = ($path[0] == DIRECTORY_SEPARATOR);
        # depart the path
        $path_parts = array_filter(explode(DIRECTORY_SEPARATOR, strtolower($path)));
        # normalizing array's parts
        $path_parts = count($path_parts)? array_chunk($path_parts, count($path_parts)) : array();
        $path_parts = count($path_parts[0])?$path_parts[0]:array();
        # UNIX fs style
        $resolved_path = $is_absolute_path ? DIRECTORY_SEPARATOR : ".";
        # WINNT fs style
        if(string::Contains($path_parts[0], ":"))
        {
            $is_absolute_path = 1;
            $resolved_path = $is_absolute_path ? "" : ".".DIRECTORY_SEPARATOR;
        }
        # do a BFS in subdirz
        foreach ($path_parts as $part)
        {
            if (!empty($part))
            {
                $target_path = $resolved_path.DIRECTORY_SEPARATOR.$part;
                if(file_exists($target_path))
                {
                    $resolved_path = $target_path;
                    continue;
                }
                $files = scandir($resolved_path);
    
                $match_found = FALSE;
    
                foreach ($files as $file)
                {   
                    if (strtolower($file) == $part)
                    {
                        $match_found = TRUE;
                        $resolved_path = $resolved_path.DIRECTORY_SEPARATOR.$file;
                        break;
                    }
                }
                if (!$match_found)
                {
                    return FALSE;
                }
            }
        }
        # cache the result
        $fc->store($target_path, $resolved_path);
        # retrun the resolved path
        return $resolved_path;
    }
    

提交回复
热议问题