Arabic / Persian characters in filename with php

Discuss coding issues, and scripts related to PHP and MySQL.
melmdoost
Posts: 20

Arabic / Persian characters in filename with php

I have a question. it is my last question.
i use persian name in file name.
when i upload a file (with persian name) and move it to upload folder, file name is illegible. i use this code:

Code: Select all

@$name = preg_replace('/(.+?)\.'. $extension .'$/i', '$1_'. $time2 .'.'. $extension, $_FILES["file"]["name"]);
$unicode = iconv('windows-1256', 'utf-8', $name);
 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $name);
but file name in upload folder is illegible yet.
like this:
123ط³طھط§ط±ظ‡_غ°غ°-غ´غ³-غ³غ² - غ±غ³غ¹غµ-غ°غ¸-غ²غ·.jpg

Admin Posts: 805
I didn't understand, what is the question?
What do you want to do with persian name when upload file?

melmdoost Posts: 20
when i upload a file with persian name, it's name will be unclear and cluttered like this:
uupload.ir/files/sdki_2016-11-17_14-11-41.jpg

While it should look like this:
uupload.ir/files/5e9b_2016-11-17_14-27-23.jpg

Admin Posts: 805
I not know how php works with persian words, but from what I tested, there is not need of using the iconv() function. Just add the header() function with utf-8 (before any output) to can output utf-8 characters.
I think this example works well:

Code: Select all

header('Content-type: text/html; charset=utf-8');

$time = date('H-i-s', time());
$name ='نور صلح.jpg';
$extension ='jpg';
$name = preg_replace('/(.+?)\.'. $extension .'$/i', '$1', $name);
$name = $name .'_'. $time .'.'. $extension;
echo $name;  // نور صلح_12-45-47.jpg

melmdoost Posts: 20
no, when i upload a file with persain name, message "echo $name; // نور صلح_12-45-47.jpg " display correctly. it display persian name correctly but when a file uploaded and moved to upload folder, it's name would be changed and displayed wrong name.

Admin Posts: 805
Maybe you have to set utf-8 encoding (or other Unicode type) in php file system settings, but I have no experience with such things.
Try look on the net for: " php upload arabic file name ".

melmdoost Posts: 20
thank you any way. your advise was useful for me.

Admin Posts: 805
A solution that not involves the file system settings, it is this:

1. When uploading the files, urlencode the names with rawurlencode().

Code: Select all

$fname = rawurlencode($_FILES['file']['name'];  // %D9%86%D9%88%D8%B1%20%D8%B5%D9%84%D8%AD.jpg
2. When fetching the files from server they are URL encoded so, use rawurldecode() to print correct names.

Code: Select all

echo rawurldecode($filename);  // نور صلح.jpg
3. Links in <a> href, and image address in <img> src are automatically translated, so for example "%D9%86" becomes a "ن " and URL ends up being incorrect since it links to incorrect filename. So, for URL /SRC, encode them back and print them ending up with something like this:

Code: Select all

echo '<a href="'. $dir. rawurlencode($filename) .'">'. rawurldecode($filename) .'</a>';

echo '<img src="'. $dir. rawurlencode($imgname) .'" alt="'. rawurldecode($imgname) .'" />';

Admin Posts: 805
- Or, in the same approach you can encode /decode the file name with base64:
1. When uploading the file, apply base64_encode() to name (without extension).

Code: Select all

$name ='نور صلح';
$ext ='jpg';
$fname = base64_encode($name) .'.'. $ext;  //filename, base64 and extension

echo $fname;  // 2YbZiNixINi12YTYrQ==.jpg
2. When fetching the file from server, apply base64_decode() to the filename (after extracting the extension) to get the correct name.

Code: Select all

$filename ='2YbZiNixINi12YTYrQ==.jpg';  //name encoded with base64, and its extension
$name_ext = explode('.', $filename, 2);  //array with [name, extension]
$name = base64_decode($name_ext[0]) .'.'. $name_ext[1];  //decode the name only

echo $name;  // نور صلح.jpg
3. In URL /SRC address add the filename as it is on server.

Code: Select all

$filename ='2YbZiNixINi12YTYrQ==.jpg';  //name encoded with base64, and its extension
$name_ext = explode('.', $filename, 2);  //array with [name, extension]
$name = base64_decode($name_ext[0]);  //decode the name only

echo '<a href="'. $dir. $filename .'">'. $name .'</a>';

echo '<img src="'. $dir. $filename .'" alt="'. $name .'" />';