search
Homephp教程php手册php上传文件中文文件名乱码的解决方法

想必很多朋友在进行utf8编码的php开发上传功能的时候,都会遇到这样的一个问题,就是上传中文文件名的文件时,文件名会变成乱码,其实我们可以用iconv函数对文件

可能会有不少朋友碰到一些问题就是上传文件时如果是英文倒好原文名不会有问题,如果是中文可能就会出现乱码了,今天我来给大家总结一下导致乱码php上传文件中文文件名乱码的原因与解决办法吧。

这几天在windows下安装了XAMPP,准备初步学习一下php的相关内容。这几天接触到了php上传文件,但是出现了一个郁闷问题,我准备上传一个excel文件,但是如果文件名是中文名就会报错。

一来二去很是郁闷,后来仔细想了想应该是文件编码的问题,我写的php文件使用的是UTF-8编码,如果没有猜错APACHE处理用的应该是GBK(当然现在我无法确定,希望高手给与指教)。想明白了这个问题,就去查找相关的教程了,索性找到了iconv这个函数。

函数原型:string iconv ( string in_charset, string out_charset, string str )

使用例子:$content = iconv("GBK", "UTF-8", $content);

这个例子的作用就是将$content从GBK转换成UTF-8编码。

乱码问题关键代码:

复制代码 代码如下:


$name=iconv("UTF-8","gb2312", $name);
move_uploaded_file($tmpname, $this->final_file_path);
$name=iconv("gb2312","UTF-8", $name);

除了这样来解决上传文件中文乱码问题,我们还可以把上传文件重新命名即可。

复制代码 代码如下:


$sFileName = "sda.php";
$sOriginalFileName = $sFileName;
$sExtension = s str($sFileName, (strrpos($sFileName, '.') + 1));//找到扩展名
$sExtension = strtolower($sExtension);
$sFileName = date("YmdHis").rand(100, 200).".".$sExtension; //这样就是我们的新文件名了,全数字的不会有乱码了哦。



以下是一些补充:

php上传中文文件名乱码解决方案

复制代码 代码如下:


 $filepath="upload/";
 $name=$filepath.$_FILES["upfile"]["name"];
 while(file_exists($name)){
   $temp=explode(".",$name);//分割字符串
    $name=$temp[0]."0".".".$temp[1];//主文件名后面加0
 }

复制代码 代码如下:


//iconv()函数是关键
  if(move_uploaded_file($_FILES["upfile"]["tmp_name"],iconv("UTF-8","gb2312",$name))){//处理...}

我的PHP编码是UTF-8,其中原因可能是因为操作系统是GBK的原因!

注意:我的服务器是windows xp、apache,,估计xp字符集是gbk。因为我的php代码保存为utf-8格式的,在给文件名命名时会出现乱码的情况,所以可以用iconv()函数将原本的utf-8格式的文件名转换为gbk格式的。

php utf8编码 上传中文文件名出现乱码的解决方法

想必很多朋友在进行utf8编码的php开发上传功能的时候,都会遇到这样的一个问题,就是上传中文文件名的文件时,文件名会变成乱码,我们可以用iconv函数对文件名进行重新编码就解决问题了。

复制代码 代码如下:


header("Content-Type:text/html;charset=utf-8");
$submit = $_POST['submit'];
if(isset($submit) && trim($submit) != ''){
$file = $_FILES['file'];
if(isset($file['tmp_name'])){
     $name = iconv('utf-8','gb2312',$file['name']); //利用Iconv函数对文件名进行重新编码
     if(move_uploaded_file($file['tmp_name'],$name)){
    echo '文件上传成功!';
    echo '图片信息:';
    print_r($file);
   }
}
}
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),