How do I use filesystem functions in PHP, using UTF-8 strings?

后端 未结 9 2284
[愿得一人]
[愿得一人] 2020-11-22 13:26

I can\'t use mkdir to create folders with UTF-8 characters:


when I

9条回答
  •  名媛妹妹
    2020-11-22 14:02

    My set of tools to use filesystem with UTF-8 on windows OR linux via PHP and compatible with .htaccess check file exists:

    function define_cur_os(){
    
        //$cur_os=strtolower(php_uname());
    
        $cur_os=strtolower(PHP_OS);
    
        if(substr($cur_os, 0, 3) === 'win'){
    
            $cur_os='windows';
    
        }
    
        define('CUR_OS',$cur_os);
    
    }
    
    function filesystem_encode($file_name=''){
    
        $file_name=urldecode($file_name);
    
        if(CUR_OS=='windows'){
    
            $file_name=iconv("UTF-8", "ISO-8859-1//TRANSLIT", $file_name);
    
        }     
    
        return $file_name;
    
    }
    
    function custom_mkdir($dir_path='', $chmod=0755){
    
        $dir_path=filesystem_encode($dir_path);
    
        if(!is_dir($dir_path)){
    
            if(!mkdir($dir_path, $chmod, true)){
    
                //handle mkdir error
    
            }
        }
        return $dir_path;
    }
    
    function custom_fopen($dir_path='', $file_name='', $mode='w'){
    
        if($dir_path!='' && $file_name!=''){
    
            $dir_path=custom_mkdir($dir_path);
    
            $file_name=filesystem_encode($file_name);
    
            return fopen($dir_path.$file_name, $mode);
    
        }
    
        return false;
    
    }
    
    function custom_file_exists($file_path=''){
    
        $file_path=filesystem_encode($file_path);
    
        return file_exists($file_path);
    
    }
    
    function custom_file_get_contents($file_path=''){
    
        $file_path=filesystem_encode($file_path);
    
        return file_get_contents($file_path);
    
    }
    

    Additional resources

    • special characters in "file_exists" problem (php)
    • PHP file_exists with accent returns false
    • http://www.developpez.net/forums/d825883/php/php-sgbd/php-mysql/mkdir-accents/
    • http://en.wikipedia.org/wiki/Uname#Table_of_standard_uname_output

提交回复
热议问题