“file_exists"中的特殊字符问题(PHP)
我使用特殊字符(瑞典字母 åäö).
I use special characters (swedish letters åäö).
现在,我有一些文件夹,其中包含分类广告的图像.文件夹按类别命名.
Now, I have some folders, which contains images for classifieds. The folders are named by category.
for ($i=1; $i<=5; $i++){
if (file_exists($big_images.$i.'.jpg')){ echo "Inne";
unlink($big_images.$i.'.jpg');
}
if (file_exists($thumb_images.$i.'.jpg')){
unlink($thumb_images.$i.'.jpg');
}
}
我的网站上最多允许 5 张图片,每张图片都以 nr 1-5 结尾.但是,我的问题是,只要文件夹名称具有特殊字符,file_exists 就会返回 false,即它找不到该文件.即使它在那里.
I allow up to 5 images on my site, each ending with a nr 1-5. However, my problem is this, whenever the folder-name has a special character, the file_exists returns false, ie it doesn't find the file. Even though it is there.
所有文件都是utf-8格式.
All documents are in utf-8 format.
这在文件夹名称中没有特殊字符时有效.
This works when there is no special characters in the folder names.
如果您需要更多输入,请告诉我
If you need more input let me know
推荐答案
服务器操作系统是什么?
What's the server OS?
如果是 Windows,您将无法访问 UTF-8 编码文件名下的文件,因为 PHP 使用的 C IO 库的 Windows 实现只会在系统默认代码页中进行对话.对于西欧安装,代码页为 1252.您可以使用 iconv 将 UTF-8 字符串转换为 cp1252:
If it's Windows, you'll not be able to access files under a UTF-8-encoded filename, because the Windows implementation of the C IO libraries used by PHP will only talk in the system default code page. For Western European installs, that's code page 1252. You can convert a UTF-8 string to cp1252 using iconv:
$winfilename= iconv('utf-8', 'cp1252', $utffilename);
(utf8_decode
也可以使用,但它会为映射到 cp1252 中 0x80-0x9F 范围的 Windows 扩展字符给出错误结果.)
(utf8_decode
could also be used, but it would give the wrong results for Windows's extension characters that map to the range 0x80-0x9F in cp1252.)
PHP 和其他使用 stdio 的程序根本无法访问名称中包含系统代码页曲目之外的字符的文件(例如西方盒子上的希腊语).有一些脚本语言可以通过 Win32 API 使用原生 Unicode 文件名,但 PHP5 不是其中之一.
Files whose names include characters outside the repertoire of the system codepage (eg. Greek on a Western box) cannot be accessed at all by PHP and other programs using the stdio. There are scripting languages that can use native-Unicode filenames through Win32 APIs, but PHP5 isn't one of them.
当然,在文件系统采用 UTF-8 编码的不同操作系统上部署时,不应使用上述步骤.(即现代 Linux.)
And of course the step above shouldn't be used when deployed on a different OS where the filesystem is UTF-8-encoded. (ie. modern Linux.)
如果您需要与 PHP 无缝跨服务器兼容,则必须避免在文件名中使用非 ASCII 字符.对不起.
If you need to seamlessly cross-server-compatible with PHP, you'll have to refrain from using non-ASCII characters in filenames. Sorry.
相关文章