低版本的PHP可能会遇到不支持中文路径的情况:
- require('http://localhost/中文路径/test.php');
- require('\中文路径\test.php');
- $file = fopen('http://localhost/中文路径/test.php');
- $file = fopen('\中文路径\test.php');
在Windows10+Apache2.4.41+PHP5.6.40环境下,测试文件编码为UTF-8,会发现除了用fopen打开URL外(3),其他(包括用fopen打开相对路径)都会报错:No such file or directory
如果对路径进行转码:
- require(iconv('utf-8', 'gbk', 'http://localhost/中文路径/test.php'));
- require(iconv('utf-8', 'gbk', '\中文路径\test.php');
- $file = fopen(iconv('utf-8', 'gbk', 'http://localhost/中文路径/test.php'));
- $file = fopen(iconv('utf-8', 'gbk', '\中文路径\test.php');
发现:(2)和(4)成功运行,(1)和(3)报错,但这次是Apache报错:HTTP/1.1 403 Forbidden
据此得出结论:
- 用require和fopen打开URL,会向Apache服务器请求资源,但Apache只支持解析UTF-8编码的路径
- 用require和fopen打开相对路径,直接与Windows文件系统交互,即ANSI编码
但是,为什么require打开UTF-8或者GBK编码的URL都会失败呢?
标签:编码,中文,PHP,require,路径,test,fopen,php From: https://www.cnblogs.com/victorique-de-blois/p/16926739.html