Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

378
Visualizações
PHP file upload with japanese filename turns into corrupted filename in DB

I have a REST API endpoint that lets you upload a file to the server. When I save a file that is named using the latin alphabet there is no issue. But when I try to save a file that has a japanese character, the file is saved and the filename in the server is okay but when I look into the database the filename is not right.

The result in my DB when saving the file:

phpMyAdmin displays one rowset with text of nonsensical characters

But when I look at the server's shell the filename is correct:

puTTY displays a proper UTF-8 filename with Hiragana in a shell

I tried changing the database collation into:

  • utf8mb4_unicode_ci
  • utf8
  • utf8_general_ci ...but the issue still persists.

Update: This is my query to insert the data:

public function setFileQuery($param,$file){
  $this->module_id = $param['moduleid'];
  $this->file_name = $file['name'];
  $this->file_size = $file['size'];

  $q = "INSERT INTO data_file
       (module_id,file_name,file_size,start_time,end_time,elapse_time,through_put)
       VALUES
       (?,?,?,?,?,?,?)";
            
  $insertStmt = $this->conn->prepare($q);
  $insertStmt->execute([
    $this->module_id,
    $this->file_name,
    $this->file_size,
    null,
    null,
    null,
    null,
  ]);
            
  Responses::http_ok();
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

  1. First, the table collation should be set utf8mb4.

  2. Second, make sure that the data field (aka column) collations are also utf8mb4.

  3. Now you may insert data (e.g. thru phpMyAdmin or thru any PHP script):

    • phpMyAdmin: phpMyAdmin INSERT query with Hiragana literal

    • PHP:

      • If you are using PDO then make sure you have set the charset in your connection, such as:
        $conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass);
        
      • If you are using MySQLi:
        $conn = mysqli_connect($servername, $username, $password, $dbname);
        
        /* change character set to utf8mb4 */
        mysqli_set_charset($conn,"utf8mb4");
        $mysqli->query("set names utf8mb4");
        

      ...and then executing the query, i.e.:

      mysqli_query( "INSERT INTO song( songname ) VALUES( 'あああ.txt' )" );
      
  4. The result will be as wanted: phpMyAdmin displaying one rowset with Hiragana text

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda