Saving UTF-8 Files on the Server Side

Asked

Viewed 355 times

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 is going on: The file name sent from the client to the server is stored correctly in the Mysql database, but when creating the file in the specified directory, the file gets several special characters displayed in the wrong way, what causes faults in the file call when this is requested by the client side.

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

  • Do not use special characters in file names. It is the best way to fix your problem.

  • I make this recommendation to the user at the instant of inserting the files. But I wonder if you have how to get around the problem in a simple way without having to give this kind of concern to the user.

  • 1

    @user3450942 if it uploads by the application itself, it is the case of sanitizing the names before saving, exchanging special and accented characters for an ASCII equivalent. If in the future it is going to download through the application, you can even save the original DB name, so it does not lose anything. Depending on the case, just convert the name to Base64, which solves. Just watch out for the overhead in the name size. In fact, if you save the original file name to a DB, you can use the database ID itself for the file name. file0000001, file000002 etc.

  • 1

    As long as the sizes of the names do not exceed 75% of the capacity of the filesystem, that’s all it takes: $convertido= strtr(base64_encode($original), '+/', '-_'); and $original = base64_decode(strtr($convertido, '-_', '+/'));

1 answer

2


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

You can use a simple substitution function like this that I’ve adapted from somewhere I can’t 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']);
  • Thank you so much for the help. I ended up researching about this function and saw that you can play in more interesting ways with her hehehe. To others who have suggested solutions, I will test what you have indicated soon and I already give the feedback. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.