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
153 views
in Technique[技术] by (71.8m points)

php - Storing special characters in DB in such a way to display properly on site and still find image with the same name

I have a site where users can change their location.

I have all of the available countries stored in a DB and an image for each of these in a folder in the same directory.

However, some countries have special characters and don't display properly or else can't find the image.

The countries in question are:

  • C?te d'Ivoire
  • ?eská republika

I tried url encoding them so it was like this: %C4%8Cesk%C3%A1+republika

I need a way to store these in the DB in such a way as that they display the name correctly on the site and find the image of the same name.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

First of all, see UTF-8 all the way through for all the things you need to do correctly to make non-ASCII characters work in your app in general.

Secondly, it's… tricky… to serve files with non-ASCII file names over the web. 1) You need to ensure that you encode all URLs for these files with percent encoding, as you already seem to do. 2) The web server will take that URL, percent-decode it to a byte string, and then ask the underlying operating/file system to look for a file with a name with that string. This is the tricky part: you won't know exactly what byte string your OS/file system uses to represent that file exactly. You would need to figure that out first, then encode the URL specifically so it will decode exactly to the correct string.

And when you move to a different server, especially if you're moving from Windows to *NIX or vice versa, you can do that all over again since those system do things very differently.

In a nutshell, it's often more hassle than it's worth, and you should store your images with ASCII-only names to avoid all that. Specifically for countries, it'd make a whole lot of sense to use the two-character country codes for the image name (e.g. "cz.jpg").


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

...