Home > Backend Development > PHP Problem > What to do if the file name is garbled when uploading files in PHP

What to do if the file name is garbled when uploading files in PHP

王林
Release: 2023-03-04 13:14:02
Original
2719 people have browsed it

Solution to garbled file names when uploading files in php: First add [header("Content-type: text/html; charset=utf-8");] to the head of the script; and then use iconv() The function converts the character encoding.

What to do if the file name is garbled when uploading files in PHP

First, add this code to the head of the php file:

(Recommended tutorial: php graphic tutorial)

header("Content-type: text/html; charset=utf-8");
Copy after login

Then define a variable:

$name = iconv('utf-8','gb2312',"upload/".$file["name"]);
Copy after login

(Video tutorial recommendation: php video tutorial)

php code:

header("Content-type: text/html; charset=utf-8");
$file = $_FILES["file"];
if($file["error"]>0){
    echo "错误:".$file["error"];
}else{
    $name = iconv('utf-8','gb2312',"upload/".$file["name"]);
    echo "文件名称:".$file["name"]."
"; echo "文件类型:".$file["type"]."
"; echo "文件大小:".($file["size"]/1024)."K
"; echo "文件临时存储的位置:".$file["tmp_name"]."
"; //保存上传的文件 if(file_exists("upload".$file["name"])){ echo $file["name"]."文件已经存在"; }else{ //如果目录不存在则将该文件上传 if(move_uploaded_file($file['tmp_name'],$name)){ // move_uploaded_file($file['tmp_name'],"upload/".$file["name"]); echo '文件上传成功!'; echo '图片信息:'; print_r($file); } }
Copy after login

The above is the detailed content of What to do if the file name is garbled when uploading files in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template