2

I have an issue with php rename function, it fail if file name is in Arabic words with spaces.

e.g.

rename(temp/أم كلثوم       ثوار - ثوار.mp3,audio/13408831061.mp3);

No such file or directory in (path)

please note: أم كلثومxxxxxxxxثوار - ثوار.mp3

xxxxxxx here atleast six spaces.

I tried urlencode() but still no result.

Any suggestion!

Amal G Jose
  • 2,486
  • 1
  • 20
  • 35
Asad kamran
  • 440
  • 10
  • 21
  • Firstly, does `audio/` exist as a directory? Secondly, what does `var_dump(file_exists('temp/أم كلثوم ثوار - ثوار.mp3'));` say? – DaveRandom Jun 28 '12 at 11:46
  • yes, audio directory exist, but filename doesnot, although i checked temp/ folder and file is there, but as filename is not legal, it cause rename to fail, as it could not find the file in temp/ – Asad kamran Jun 28 '12 at 12:02
  • all files with legal name are renamed accurately. just arabic name with spaces or invalid chars are failed. – Asad kamran Jun 28 '12 at 12:03
  • of course the line of code you reported is missing 4 quotes, right? And what do you mean by invalid chars? – Walter Tross Jun 28 '12 at 12:05
  • there are many spaces in file name, there may be some invisible chars. also i found كلثوم ثوار - ثوار.mp3 in temp/ directory using FTP software. For missing quotes i passes variables. here is code. $uploaddir='audio/'.$file_name; $bg_temp='temp/'."$file_id"; $move=rename($bg_temp, $uploaddir); – Asad kamran Jun 28 '12 at 12:09
  • what FTP writes looks like a UTF-8 encoded name seen as if it were ISO-8859-1 encoded. Looking at this (wrong) output it seems to me that the name is stored in "read" direction, i.e., from right to left (because the last sequence of UTF-8 chars also appears before in the string). You should check whether $file_name is correctly UTF-8 encoded too. – Walter Tross Jun 28 '12 at 12:16
  • I checked: what I wrote about FTP is correct, and the filename is the one (in Arabic) that you reported. I'm going to write a few lines of code for checking the content in an answer (comments are not so practical for code) – Walter Tross Jun 28 '12 at 12:28

2 Answers2

2

This Sample Should Helps , I Test Bellow Code And It Works In Windows For Arabic/Persian Names:

$newname  = $filename = iconv("utf-8", "cp1256","گچپژ");
echo rename("1.txt", $newname);
MSS
  • 3,520
  • 24
  • 29
1

To check that your $file_id is correct, do this on a Unix-like system:

echo "<pre>\n"; // for HTML output only
system("echo -n '$file_id' | od -tx1");

With this value, which should be your reported filename (you can do an echo to check)

$file_id = "\xD9\x83\xD9\x84\xD8\xAB\xD9\x88\xD9\x85 \xD8\xAB\xD9\x88\xD8\xA7\xD8\xB1 - \xD8\xAB\xD9\x88\xD8\xA7\xD8\xB1.mp3";

the output is

0000000 d9 83 d9 84 d8 ab d9 88 d9 85 20 d8 ab d9 88 d8
0000020 a7 d8 b1 20 2d 20 d8 ab d9 88 d8 a7 d8 b1 2e 6d
0000040 70 33
0000042

(as you can see, the sequence ending with a B1 repeats itself, the last 4 bytes are .mp3)

In the directory where the file should be you can issue a

ls -1 | od -tx1

and you will see all filenames separated by 0a which are newlines.

If both outputs match, the problem is somewhere in PHP or in the system (both unlikely), if they don't you will have to chase the problem in your code, starting from the encoding, which must be UTF-8 everywhere.

Walter Tross
  • 12,237
  • 2
  • 40
  • 64
  • I do as you suggest and found the urlencoded file names were different in temp/ directory and rename function source filename. So i analyze the code in back sequence and found that basename($_FILES['songs']['name']) was applied, it cause the filename in temp/ directory different. I removed it and now filenames are same, and files can be renamed accurately. Thanks Walter Tross – Asad kamran Jun 28 '12 at 12:57