5

I am trying to upload file that have Arabic name like (مرحبا بكم), but when I upload it to server the string is not correct, it shows characters like that (ريÙ-Ù).

So, how can I upload files and keep the correct Arabic name?

<form action="up.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

PHP File :

 <?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
user3056538
  • 73
  • 1
  • 4
  • just some advice but figure out the octal / ascii / what ever the encoding chart is in and check it against the values on your computer it could be that its uploaded correctly and you are viewing it incorrectly. dd and xxd are your friends, I'm sorry i could not offer more then a suggestion and not actually help! GL – Tegra Detra Dec 11 '13 at 02:27
  • Maybe this helps: http://stackoverflow.com/questions/15055192/why-does-windows-need-to-utf8-decode-filenames-for-file-get-contents-to-work – Hardy Dec 11 '13 at 02:29
  • 2
    Ensure that your output is utf-8: `header('Content-Type: text/html; charset=utf-8');` – Rob M. Dec 11 '13 at 02:44
  • 1
    Make sure the filesystem you're uploading content to is UTF8 enabled – Zarathuztra Dec 11 '13 at 03:06
  • @Rob M. by writing header('Content-Type: text/html; charset=utf-8'); it do not work, do not upload with arabic language. – Muhammad Rizwan Kaim Khani Dec 11 '13 at 06:11
  • What about adding `setlocale(LC_ALL, 'ar_AE.utf8');` to the top of your script? – Rob M. Dec 11 '13 at 06:16
  • 2
    Possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/q/279170/1255289) – miken32 Mar 23 '17 at 04:31

4 Answers4

1

I would enable unicode (utf8) in the both the web page and the filesystem.

Osama Al-Maadeed
  • 5,654
  • 5
  • 28
  • 48
0

Why cant you just encode and decode the filename using base64.

getfile name->
$encoded_fname = base64_encode("arabic_filename")

upload file->move_uploaded_file with encoded filename;

move_uploaded_file($_FILES["file"]["tmp_name"],"path/to/move/" . $encoded_fname);

decode if you want when you download.

Hope this gives you an idea.

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
ganesh
  • 780
  • 7
  • 19
0

Now you may try like my below code.

HTML FILE

<html>
<head></head>
<body>
<form action="up.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="FILE" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

PHP FILE (up.php)

make the folder upload, on your root directory.

<?php 
if(isset($_POST['submit'])) {
   $file  = $_FILES['file']['name'];
   $encoded_fname = base64_encode($file);
   if($file) {
        $move = move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $encoded_fname);
        if($move) {
        echo "file uploaded";
        }
   }
}
?>

Result read files(watch.php)

$dir = "upload/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if(mb_check_encoding($file)) {
            echo "filename: .".base64_decode($file)."<br />";
            }
            else {
                echo $file . "<br>";
            }
        }
        closedir($dh);
    }
}

So you upload file in encode base64 and then get the result by decode it.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

i had the same problem on a site i was working on and i had spend a long long time on this so just for share the knowledge :) u dont need encode or changing the charset or any thing is after the submit to rename the file by timestamp it work for me perfectly like this

$file = $_FILES['file']['name'][$f]; //the file in arabic
///////////////////////////////////////////////////////////
$arr = explode(".", $file, 2);
echo $first = $arr[0]; // the name without the extension and the dot
///////////////////////////////////////////////////////////
//$data = $name;
$whatIWant = substr($data, strpos($data, ".") + 1);    
$file_upload =time().".".$whatIWant; //name of the upload file

`
hope it help any lost soul :) and this is a the full page

enter link description here