How do I upload php file correctly when it has special characters or accents?

Asked

Viewed 193 times

0

Hello, I am trying to upload an audio file in php, the problem is that the file should be moved to a name with accentuation, til, for example. Example: Save the file named Sebastião.mp3, but php at the time of moving the file upload moves with the name of Sebastiã£o.mp3

look at the upload.php file

$target_dir = "../../mp3/";
$nomeF="Sebastião"; <- aqui eu pego no banco de dados o nome...
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$target_file2=utf8_encode($target_dir).$nomeF.utf8_encode(".mp3"); <- tentei codificar o arquivo mas nao deu resultado
ou
$target_file2=$target_dir.$nomeF.".mp3";
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if ($_FILES["fileToUpload"]["size"] > 59900000) {
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "mp3" ) {
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {

    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file2)) {

        } else {

        }
    }

However, as I said, the file saved wrong. Someone knows what to do?

1 answer

0

An interesting approach is to create a hash using MD5 to be the file name in your directory and save it in the database linking to the real name (with accents and special characters)

echo md5('apple');
//1f3870be274f6c49b3e31a0c6728957f

I see as a safe and organized way.

  • So, as I had need to solve the problem fast, I took a function that removes the accents, and saves the file without the accents, so don’t give me problems when saving or reading... Ai became easy, just use a function that does this both in php, which I use to save mp3, and the javascript that I use to read the file, as well as the python that I use to read the file in a script : )

Browser other questions tagged

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