douzao1119 2011-09-07 15:39
浏览 52
已采纳

在php中使用UTF-8字符集

I have been struggling with the UTF-8 charset for quite a while now, and I am still confused about some things.

I have a web page which allow clients to create HTML files and directories on server. The directory name can be in any language. Adiós, días, chapter, level etc. The directories created are later on used as a URL for the HTML files created. Let’s say the user created a directory Adiós and then a file called welcome.html. To view this file, the client clicks a link and for that I get the directory and file name to create a path Adiós/welcome.html. Now I am confused about these things.

  1. When making the directory in php, should I urlencode() every file and directory name?

  2. If I do urlencode the directory name, will the browser be able to open my HTML page? Instead of href="Adiós/welcome.html" it will be href="Adi%C3%B3s/welcome.html".

  3. There’s sometimes an image on my web page which I will src as "Adi%C3%B3s/ing.jpg"; is this going to work?

  4. Should the url in address bar show non‐ASCII characters?

I actually urlencode()d everything but ran into issues as described in point 2 and 3, so I wanted to know what the right approach is for directory naming when working with languages other than English!

  • 写回答

3条回答 默认 最新

  • dongwen7813 2011-09-07 15:49
    关注

    If you save the names urlencoded in the filesystem, you must double urlencode the links and image sources if you want to access them directly, bypassing PHP. Alternatively, you could save the names without any kind of urlencoding, in which case the links would need one pass. However, this last option isn't available on Windows, where Unicode is not supported in the filesystem functions.

    Alternatively, if you still want to bypass PHP, you can use rewrite rules to reencode the names once they have urldecoded by Apache.

    Finally, you should take note that your approach is dangerous -- difficult to get right without security implications. You should consider have a single PHP file serving your pages and saving them in a database. You could still keep pretty filenames by using the PATH_INFO variable. You could also add a caching layer in front of PHP if performance becomes an issue with this solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据