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!

7
  • Firstly, does audio/ exist as a directory? Secondly, what does var_dump(file_exists('temp/أم كلثوم ثوار - ثوار.mp3')); say? Commented Jun 28, 2012 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/ Commented Jun 28, 2012 at 12:02
  • all files with legal name are renamed accurately. just arabic name with spaces or invalid chars are failed. Commented Jun 28, 2012 at 12:03
  • of course the line of code you reported is missing 4 quotes, right? And what do you mean by invalid chars? Commented Jun 28, 2012 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); Commented Jun 28, 2012 at 12:09

2 Answers 2

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

It supposed to be : echo rename($newname,"1.txt");
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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.