Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

html - 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');
        }
    }

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What's the server OS?

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 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.)

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.

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.)

If you need to seamlessly cross-server-compatible with PHP, you'll have to refrain from using non-ASCII characters in filenames. Sorry.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...