Home  >  Article  >  Backend Development  >  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

王林
王林Original
2020-08-14 14:26:522624browse

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");

Then define a variable:

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

(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); } }

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!

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

Related articles

See more