“file_exists”中的特殊字符问题(php) [英] special characters in "file_exists" problem (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结尾。
然而,我的问题是这样的,每当folder-name有一个特殊字符,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.
如果您需要更多输入,请告诉我
If you need more input let me know
推荐答案
什么是服务器操作系统?
What's the server OS?
如果是Windows,您将无法访问UTF- filename,因为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
它会给出错误的结果,Windows的扩展字符映射到cp1252中的范围0x80-0x9F。)
(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使用native-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.
这篇关于“file_exists”中的特殊字符问题(php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!