Home  >  Article  >  Backend Development  >  What should I do if the file name uploaded by php is garbled?

What should I do if the file name uploaded by php is garbled?

藏色散人
藏色散人Original
2020-08-12 10:49:592786browse

Solution to the garbled file name in php upload: First open the "Upload.html" file; then add the statement "enctype="multipart/form-data""; then open the "upload.php" file; and finally correct The file name can be forced to be transcoded.

What should I do if the file name uploaded by php is garbled?

Recommended: "PHP Video Tutorial"

PHP upload files and Chinese names with garbled characters

About PHP file upload

In the front-end HTML page, the form is as follows

Upload.html

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="2621114">
    <input type="file" required name="upload_file">
    <input type="submit" value="提交">
</form>
</body>
</html>

Note

enctype=" "multipart/form-data" must be written. This tells the browser what you are uploading

02ad864ff822bc5116f29be2aa40bace The front-end setting file is large Maximum value

Backend upload.php

<?php
if (is_uploaded_file($_FILES[&#39;upload_file&#39;][&#39;tmp_name&#39;])){
    $upfile = $_FILES[&#39;upload_file&#39;];
    //print_r($upfile);
    $name = $upfile[&#39;name&#39;];//获取文件名
    $type = $upfile[&#39;type&#39;];//文件类型
    $size = $upfile[&#39;size&#39;];//文件大小
    $tmp_name = $upfile[&#39;tmp_name&#39;];//服务器存储的临时名称
    $error = $upfile[&#39;error&#39;];//错误信息
    switch ($type){
        case &#39;image/png&#39;:
            $ok=1;
            break;
        case &#39;image/jpg&#39;:
            $ok=1;
            break;
        case &#39;image/jif&#39;:
            $ok=1;
            break;
        case &#39;image/jpeg&#39;:
            $ok=1;
            break;
    }
    if ($ok && $error == 0){
        move_uploaded_file($tmp_name,&#39;./upload/&#39;.iconv("UTF-8", "gbk",$name));
        echo &#39;文件上传成功&#39;;
    }else{
        echo &#39;文件上传失败&#39;;
    }
}

When uploading, PHP receives an array of information about the file. This information can be in the super global array $_FILES turn up.

For example: If the name of the file input box in the form is upload_file, then all information about the file is included in the array $_FILES['upload_file'].

is_uploaded_file — Determine whether the file is uploaded through HTTP POST

move_uploaded_file — Move the uploaded file to a new location

bool move_uploaded_file ( string $filename , string $destination )

When encountering a Chinese file name, correct The file name is forced to transcode iconv("UTF-8", "gbk",$name) to convert UTF8 to gbk, so that there will be no garbled characters

The above is the detailed content of What should I do if the file name uploaded by php is garbled?. 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
Previous article:How to delete php on macNext article:How to delete php on mac