Rumah >pembangunan bahagian belakang >tutorial php >php上传中文文件文件名乱码有关问题
php上传中文文件文件名乱码问题
php上传文件是最最基础的一个技术点,但是深入进去也有不少问题需要解决,这不,上传中文文件后,文件名变成了乱码。
下面是问题代码,很简单:
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 3</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="upload_file.php"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #008080;"> 5</span> <span style="color: #ff0000;">enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;"><span style="color: #800000;">label </span><span style="color: #ff0000;">for</span><span style="color: #0000ff;">="file"</span><span style="color: #0000ff;">></span>Filename:<span style="color: #0000ff;"></span><span style="color: #800000;">label</span><span style="color: #0000ff;">></span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="file"</span> <span style="color: #0000ff;">/></span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;"><span style="color: #800000;">br </span><span style="color: #0000ff;">/></span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="Submit"</span> <span style="color: #0000ff;">/></span><span style="color: #008080;">10</span> <span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span> <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #008080;">13</span> <span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span>
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$_FILES</span>["file"]["error"] > 0<span style="color: #000000;">)</span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">echo</span> "Return Code: " . <span style="color: #800080;">$_FILES</span>["file"]["error"] . "<br>"<span style="color: #000000;">;</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">}</span><span style="color: #0000ff;">else</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">echo</span> "Upload: " . <span style="color: #800080;">$_FILES</span>["file"]["name"] . "<br>"<span style="color: #000000;">;</span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">echo</span> "Type: " . <span style="color: #800080;">$_FILES</span>["file"]["type"] . "<br>"<span style="color: #000000;">;</span><span style="color: #008080;">10</span> <span style="color: #0000ff;">echo</span> "Size: " . (<span style="color: #800080;">$_FILES</span>["file"]["size"] / 1024) . " Kb<br>"<span style="color: #000000;">;</span><span style="color: #008080;">11</span> <span style="color: #0000ff;">echo</span> "Temp file: " . <span style="color: #800080;">$_FILES</span>["file"]["tmp_name"] . "<br>"<span style="color: #000000;">;</span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">if</span> (<span style="color: #008080;">file_exists</span>("upload/" . <span style="color: #800080;">$_FILES</span>["file"]["name"<span style="color: #000000;">]))</span><span style="color: #008080;">14</span> <span style="color: #000000;"> {</span><span style="color: #008080;">15</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_FILES</span>["file"]["name"] . " already exists. "<span style="color: #000000;">;</span><span style="color: #008080;">16</span> <span style="color: #000000;"> }</span><span style="color: #008080;">17</span> <span style="color: #0000ff;">else</span><span style="color: #008080;">18</span> <span style="color: #000000;"> {</span><span style="color: #008080;">19</span> <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"],<span style="color: #008080;">20</span> "upload/" . <span style="color: #800080;">$_FILES</span>["file"]["name"]);<br> }<br> }
上传了一个文件名为“测试数据.txt”的文件,oh ho,文件是传上去了,但是文件名为乱码。
网上搜索一下解决方案,将
<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"], "upload/" . <span style="color: #800080;">$_FILES</span>["file"]["name"]);
改成
<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"],"upload/" . <span style="color: #008080;">iconv</span>("UTF-8","gbk",<span style="color: #800080;">$_FILES</span>["file"]["name"]));
结果发现iconv函数返回值为false。
查一下函数手册,发现第二个参数有特别的用法,简单翻译一下就是我可以在编码的后面追加//TRANSLIT 或 //IGNORE ,前者会将无法翻译的字符转成最接近的字符,后者就是直接忽略不能转化的字符。
试一下:
<span style="color: #008080;">1</span> <span style="color: #008080;">var_dump</span>( <span style="color: #008080;">iconv</span>("UTF-8","gbk//TRANSLIT",<span style="color: #800080;">$_FILES</span>["file"]["name"<span style="color: #000000;">]));</span><span style="color: #008080;">2</span> <span style="color: #008080;">var_dump</span>( <span style="color: #008080;">iconv</span>("UTF-8","gbk//IGNORE",<span style="color: #800080;">$_FILES</span>["file"]["name"]));
结果:
bool(false) string(4) ".txt"
也就是说中文都没法转化,甚至连接近的字符都没有,看来网上介绍的方法也并非万能。
猜测一下,也许我的系统在创建中文文件的时候会乱码,于是我将代码改写了一下:
<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"], "upload/测试数据.txt");
结果创建成功,没有乱码。。。也就是说不是系统问题。
想一下,我的php文件本身是utf8编码的,那么
<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"],"upload/测试数据.txt");
这个语句肯定使用的是utf8编码,那么之前上传的文件名肯定就不是utf8编码了,那么以下的语句肯定是错误的,因为源字符串本身就不是utf8编码的:
<span style="color: #008080;">iconv</span>("UTF-8","gbk//TRANSLIT",<span style="color: #800080;">$_FILES</span>["file"]["name"]);
使用函数检查源字符串的编码:
<span style="color: #008080;">1</span> <span style="color: #800080;">$e</span>=mb_detect_encoding(<span style="color: #800080;">$text</span>, <span style="color: #0000ff;">array</span>(‘UTF-8’, ‘GBK’,<span style="color: #000000;">’gb2312’));</span><span style="color: #008080;">2</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$e</span>;
结果是CP936,也就是源字符串编码是GBK。
试一下
<span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>["file"]["tmp_name"],"upload/" . <span style="color: #008080;">iconv</span>("gbk","UTF-8",<span style="color: #800080;">$_FILES</span>["file"]["name"]));
问题解决,不再乱码
实际上还有一种解决办法,就是在html文件的head标签中间加入
<span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="Content-Type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html; charset=utf-8"</span> <span style="color: #0000ff;">/></span></span>
从而使编码保持统一,也就不需要再转码了