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 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流