Saving UTF-8 Files on the Server Side

1

Help me with this code here in PHP:

    <!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Atualização de dados</title>
    <link rel="stylesheet" type="text/css" href="upload.css"/>
  <link href='https://fonts.googleapis.com/css?family=Varela+Round|Raleway:900' rel='stylesheet' type='text/css'>
    <script type:"text/javascript" src="../jquery-2.1.4.min.js"></script>      
</head>
<body>
<div><p id="header"><b>ATUALIZAÇÃO DE CONTEÚDO</b></p></div>

<?php
ini_set('default_charset','UTF-8');
require("../dbconnect.inc.php");


$assunto = isset($_POST['ass']) ? $_POST['ass'] : FALSE;
$aula = isset($_POST['aul']) ? $_POST['aul'] : FALSE;
$qURL = isset($_POST['qURL']) ? $_POST['qURL'] : FALSE;
$vid_nome = $_FILES['vidUpdt']['name'];
$aud_nome = $_FILES['audUpdt']['name'];
$img_nome = $_FILES['imgUpdt']['name'];
$txt_nome = $_FILES['txtUpdt']['name'];


//Diretórios raíz de uma aula
$_UPAul['pasta']['video'] = 'video/' . $assunto . '/' . $aula;
$_UPAul['pasta']['audio'] = 'audio/' . $assunto . '/' . $aula;
$_UPAul['pasta']['texto'] = 'texto/' . $assunto . '/' . $aula;
$_UPAul['pasta']['imagem'] = 'imagem/' . $assunto . '/' . $aula;

//UPDATE DO VIDEO
if(empty($vid_nome)==FALSE){
  ExcluiDir($_UPAul['pasta']['video']);
    if(!(file_exists('video/' . $assunto))){
    mkdir('video/' . $assunto . '/' . $aula,0777,true);
}else{
    mkdir('video/' . $assunto . '/' . $aula,0777);
    }
    $_UP['pasta']['video'] = 'video/' . $assunto . '/' . $aula . '/';
    $vid_URL = $_UP['pasta']['video'] . $vid_nome;
    if (move_uploaded_file($_FILES['vidUpdt']['tmp_name'], $_UP['pasta']['video'] . $vid_nome)) {
  // Upload efetuado com sucesso, exibe uma mensagem e um link para o arquivo
  echo "<p align='center' style='color:#8F8F8F'>Upload do video efetuado com sucesso!</p>"; 
} else {
  // Não foi possível fazer o upload, provavelmente a pasta está incorreta
  echo "<p align='center' style='color:#8F8F8F'>Não foi possível enviar o arquivo de video, tente novamente.</p> <br>";
}

}
?>

What's happening: The name of the file sent from the client to the server is stored correctly in the MySQL database, but at the time of creating the file in the specified directory, the file gets several special characters displayed the wrong way, which causes in when it is requested by the client side.

I've already put all the files in UTF-8 encoding. How can I solve this?

    
asked by anonymous 19.11.2015 / 14:49

1 answer

2

In this case, it is best to modify the code, avoiding special characters.

You can use a simple replacement function like this that I have adapted from somewhere I can not remember anymore

function sanitize_filename($filename) {
    $string = htmlentities($string, ENT_QUOTES, 'UTF-8');
    $string = preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', $string);
    $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
    $string = preg_replace(array('~[^0-9a-z]~i', '~[ -]+~'), '_', $string);     
    return $string;
}

And put in your code this way

$assunto = isset($_POST['ass']) ? sanitize_filename($_POST['ass']) : FALSE;
$aula = isset($_POST['aul']) ? sanitize_filename($_POST['aul']) : FALSE;
$qURL = isset($_POST['qURL']) ? sanitize_filename($_POST['qURL']) : FALSE;
$vid_nome = sanitize_filename($_FILES['vidUpdt']['name']);
$aud_nome = sanitize_filename($_FILES['audUpdt']['name']);
$img_nome = sanitize_filename($_FILES['imgUpdt']['name']);
$txt_nome = sanitize_filename($_FILES['txtUpdt']['name']);
    
19.11.2015 / 21:11