Home  >  Article  >  Backend Development  >  How to solve the problem that php cannot upload Chinese files

How to solve the problem that php cannot upload Chinese files

藏色散人
藏色散人Original
2021-07-17 09:34:572235browse

php不能上传中文的解决方法:1、通过“iconv("GBK", "UTF-8", $content);”语句将中文字符编码转换一下;2、将文件重命名。

How to solve the problem that php cannot upload Chinese files

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php不能上传中文?PHP 上传文件名中带中文的文件失败问题

问题产生的原因是:中文乱码问题

php文件为utf-8编码方式,

解决方案1:将中文字符编码转换一下。

函数原型:

string iconv ( string in_charset, string out_charset, string str )

使用例子:

$content = iconv("GBK", "UTF-8", $content);

代码如下:

$name=iconv("UTF-8","gb2312", $name);
move_uploaded_file($tmpname, "upload/".$name); 
$name=iconv("gb2312","UTF-8", $name);

解决方案2:文件重命名

//保存在服务器的名字则是时间戳,加文件后缀名
$saveName=time().$fileExtensions;
//取得服务器的目录的绝对路径。
$basepath=str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/";
//然后则保存这个上传文件
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$saveName);

推荐学习:《PHP视频教程

The above is the detailed content of How to solve the problem that php cannot upload Chinese files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn