-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow file helper perform character set conversion #4322
Comments
What's your use case? |
I'm creating pdf reports based on user input and / or value of database fields. I know it's possible to use some (numeric) key as filenames and sustitute them later by stored strings. But then you can't use extensions which will display (and download) folder contents. |
Most of nowaday FS are unicode-aware so characters like |
@samdark, I don't know what exactly goes wrong but the following code $dir = 'Test Characters aöü';
$directory = '../../'. $dir;
\yii\helpers\FileHelper::createDirectory($directory); procuces such directory. Content of writtten files is ok. I checked php.ini which has a Did I miss something in the configuration of my environment or is it required to convert non ascii characters? |
I don't think this has anything to do with the file helper. The helper should not attempt to do charset conversion. |
Yes. I just want to make sure it doesn't contain any surprises... |
OK, verified that it's Windows issue and |
Your' re right. It is a windows issue. Plain php mkdir or dos cmd mkdir has same result. I searched a lot but the only solution I found is the charset conversion. Would love to see how other developers solved this issue. PS: I'm running a NTFS filsystem which has UTF-8 character set as default. |
You have 2 ways of dealing with it:
|
@samdark, thanks for your answer. So there are basicly 3 choices.
A good explaination is given here: The following code might help to investigate which kind of translation meets most of users needs. Combining them with file helper functions might be handy but I accept that this should handled by the developer. $dir = 'Test characters € äöü';
$dir = 'Test characters äöü';
$dirs = [
[$dir, 'string'],
[utf8_decode($dir), 'utf8_decode'], // fails on € sign
[urlencode($dir), 'urlencode'],
[urldecode($dir), 'urldecode'],
[htmlentities($dir), 'htmlentities'],
[iconv("UTF-8", "ISO-8859-1//TRANSLIT", $dir) , 'iconv'],
[iconv("UTF-8", "ISO-8859-1//TRANSLIT", urldecode($dir)), 'iconv urldecoded'],
// [mb_convert_encoding($dir), 'iso-8859-15'), 'mbConvert'] // mkdir errror
[UConverter::transcode($dir, 'ISO-8859-1', 'UTF-8'), 'transcodeFromUtf8'], // fails on € sign
[transliterator_transliterate('Any-Latin', $dir), 'transliterate any latin'],
[transliterator_transliterate('Any-Latin; Latin-ASCII', $dir), 'transliterate any latin and latin ascii'],
[Inflector::slug($dir), 'Inflector Slug']
];
foreach ($dirs as $key =>$dir) {
$directory = '../../test/' . $key . ' - ' . $dir[0] .' --- ' .$dir[1];
echo '<br>' . $directory;
if (!is_dir($directory)) {
FileHelper::createDirectory($directory);
}
} |
Using non ascii characters in file or directory names leads to unwanted string conversion.
Allowing a character set conversion via
could solve most of the pain.
File helper functions could be enhanced to accept the output charset as a parameter or via application config.
The text was updated successfully, but these errors were encountered: