0

Im doing a webpage and have a problem with file upload, that changes the file name umlauts into a weird name.

For example when i upload a file called "töö.docx" and look at the name in the uploaded folder, it shows me this "tƶƶ.docx".

When i call out the name of the file in index.php it shows me the correct name "töö.docx".

But after i go into the upload folder and change the name "tƶƶ.docx" manually into "töö.docx" and than call out the name of the file in index.php, it shows me "t��.docx" which is wrong.

Here is the code for upload in index.php:

<form method="post" enctype="multipart/form-data">
  <strong>File upload:</strong>
  <small>(max 8 Mb)</small>
  <input type="file" name="fileToUpload" required>
  <input type="submit" value="Upload" name="submit">
</form>

And here is the upload controller code:

$doc_list = array();
   foreach (new DirectoryIterator('uploads/') as $file)
{
   if ($file->isDot() || !$file->isFile()) continue;
   $doc_list[] = $file->getFilename();
}

$target_dir = "uploads/";
$target_file = $target_dir . basename( isset($_FILES["fileToUpload"]["name"]) ? $_FILES["fileToUpload"]["name"] : "");
$file = isset($_FILES["fileToUpload"]) ? $_FILES["fileToUpload"] : "";
$up_this = isset($_FILES["fileToUpload"]["tmp_name"]) ? $_FILES["fileToUpload"]["tmp_name"] : "";
$file_name = isset($_FILES["fileToUpload"]["name"]) ? $_FILES["fileToUpload"]["name"] : "";

if (!empty($file)) {
    if(isset($_POST["submit"])) {
        if (file_exists($file_name)) {
            echo "File already exists.";
            exit;
        } else {
            $upload =  move_uploaded_file($up_this, $target_file);
            if ($upload) {
                echo "File ". '"' . basename($file_name). '"' . " has been uploaded";
            } else if (!$upload) {
                echo "Could not upload file";
                exit;
            }
        }
    }
}

I use the variable $doc_list to call out the names of the documents in folder in index.php:

<div>
    <?php if (!empty($doc_list)) foreach ($doc_list as $doc_name) { ?>
        <tr>
            <td><?= $doc_name ?></td>
        </tr>
    <?php } ?>
</div>

I've set the website charset into utf-8. and i still don't know why it's not displaying the correct file name with umlauts.

10
  • 1
    Have you tried wrapping the filename in utf8_encode(..) Commented May 20, 2015 at 23:06
  • That fixed the file name on form but not the filename in the folder Commented May 20, 2015 at 23:11
  • 1
    When you say "look at the name in the uploaded folder", how are you doing that? Also, how is $doc_list populated? Commented May 20, 2015 at 23:14
  • "look at the name in the uploaded folder" - by that i mean when i go into that folder and the umlauts of the file name have been replaced by thoseweird characters. $doc_list is an array from the controller that holds all the file names in uploads folder. Commented May 20, 2015 at 23:19
  • 1
    possible duplicate of how to iterate over non-English file names in PHP Commented May 21, 2015 at 0:14

2 Answers 2

1

Try to add accept-charset="UTF-8" like this:

<form method="post" enctype="multipart/form-data" accept-charset="UTF-8">
Sign up to request clarification or add additional context in comments.

Comments

0

Try to add accept-charset="ISO-8859-1" instead "UTF-8":

<form method="post" enctype="multipart/form-data" accept-charset="ISO-8859-1">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.