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

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

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


when I

相关标签:
9条回答
  • 2020-11-22 13:47

    Using the com_dotnet PHP extension, you can access Windows' Scripting.FileSystemObject, and then do everything you want with UTF-8 files/folders names.

    I packaged this as a PHP stream wrapper, so it's very easy to use :

    https://github.com/nicolas-grekas/Patchwork-UTF8/blob/lab-windows-fs/class/Patchwork/Utf8/WinFsStreamWrapper.php

    First verify that the com_dotnet extension is enabled in your php.ini then enable the wrapper with:

    stream_wrapper_register('win', 'Patchwork\Utf8\WinFsStreamWrapper');
    

    Finally, use the functions you're used to (mkdir, fopen, rename, etc.), but prefix your path with win://

    For example:

    <?php
    $dir_name = "Depósito";
    mkdir('win://' . $dir_name );
    ?>
    
    0 讨论(0)
  • 2020-11-22 13:54

    Try CodeIgniter Text helper from this link Read about convert_accented_characters() function, it can be costumised

    0 讨论(0)
  • You could use this extension to solve your issue: https://github.com/kenjiuno/php-wfio

    $file = fopen("wfio://多国語.txt", "rb"); // in UTF-8
    ....
    fclose($file);
    
    0 讨论(0)
  • 2020-11-22 13:59

    The problem is that Windows uses utf-16 for filesystem strings, whereas Linux and others use different character sets, but often utf-8. You provided a utf-8 string, but this is interpreted as another 8-bit character set encoding in Windows, maybe Latin-1, and then the non-ascii character, which is encoded with 2 bytes in utf-8, is handled as if it was 2 characters in Windows.

    A normal solution is to keep your source code 100% in ascii, and to have strings somewhere else.

    0 讨论(0)
  • 2020-11-22 13:59

    I don't need to write much, it works well:

    <?php
    $dir_name = mb_convert_encoding("Depósito", "ISO-8859-1", "UTF-8");
    mkdir($dir_name);
    ?>
    
    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题