user3056538
user3056538

Reputation: 73

upload file that have arabic name

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"];
  }
?>

Upvotes: 5

Views: 4908

Answers (4)

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.

Upvotes: 0

Abubaker Hmd
Abubaker Hmd

Reputation: 3

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

Upvotes: 0

Osama Al-Maadeed
Osama Al-Maadeed

Reputation: 5695

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

Upvotes: 1

ganesh
ganesh

Reputation: 808

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.

Upvotes: 0

Related Questions