php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46990 Passing UTF8 strings to filesystem functions produce wrong filenames
Submitted: 2009-01-02 08:03 UTC Modified: 2010-06-13 18:42 UTC
Votes:50
Avg. Score:4.4 ± 0.9
Reproduced:44 of 44 (100.0%)
Same Version:20 (45.5%)
Same OS:24 (54.5%)
From: alex dot bazan at concatel dot com Assigned: pajoye (profile)
Status: Wont fix Package: Filesystem function related
PHP Version: 6* OS: win32 only - Windows XP
Private report: No CVE-ID: None
 [2009-01-02 08:03 UTC] alex dot bazan at concatel dot com
Description:
------------
Under Windows, when I use fpoen() or mkdir() with a UTF8 encoded string, the file or directory name is not converted to the filesystem encoding and thus the file or directory is created with a wrong file name.

Reproduce code:
---------------
<?php mkdir('&#26085;&#26412;&#35486;'); ?>

Expected result:
----------------
Directory &#26085;&#26412;&#35486; should be created

Actual result:
--------------
Directory 日本語 is created

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-02 08:06 UTC] alex dot bazan at concatel dot com
The UTF string did not get saved correctly in the bug report. It was a japanese string.
 [2009-01-03 13:25 UTC] jani@php.net

 [2009-01-09 10:54 UTC] alex dot bazan at concatel dot com
For anyone stumbling upon this bug, i wrote a workaround building a wrapper for filesystem functions. This workaround will will not fix the issue completely but will work with latin compatible characters when using UTF8 as the string encoding. People using other characters (japanese, chinese, hebrew ...) will still have problems.


CODE:


/**
 * Wrapper for PHP filesystem functions
 */
class Fsw {

    /**
     * Returns the filename for use with filesystem functions
     */
    static function setName ($file) {
        if (DIRECTORY_SEPARATOR=="\\") {
            $file=utf8_decode($file);
        }
        return $file;
    }

    /**
     * Encondes the filename returned by filesystem functions
     */
    static function getName ($file) {
        if (DIRECTORY_SEPARATOR=="\\") {
            $file=utf8_encode($file);
        }
        return $file;
    }


    static function file_exists ($filename) {
        return file_exists(self::setName($filename));
    }

    static function fopen ($filename,$mode) {
        return fopen(self::setName($filename),$mode);
    }

    static function opendir ($dirname) {
        return opendir(self::setName($dirname));
    }

    static function readdir ($dir_handle) {
        $file=readdir($dir_handle);
        if ($file) {
            // check if file is found before converting it's
            // name or we will convert bool(false) to string
            $file=self::getName($file);
        }
        return $file;
    }

// just create any other filesystem function you might use in your code here

}
 [2009-01-09 11:24 UTC] pajoye@php.net
Can't be fixed in 5.2 neither in 5.3. It will work smoothly in 6.x (unicode support).
 [2010-06-13 18:42 UTC] felipe@php.net
-Status: Assigned +Status: Wont fix
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 07:01:29 2024 UTC