Php: How To Copy Files That Have 16-bit File Names?

liunx

Guest
I have written a PHP script that I use to back up my internal hard drive to an external hard drive. I use Windows XP, and my internal hard drive is NTFS formatted.<br /><br />Omitting the details, I'm basically using:<br /><br />$file = readdir($dir);<br />$original = $C_drive_path.'\\'.$file;<br />$bkup = $E_drive_path.'\\'.$file;<br />copy($original, $bkup);<br /><br />The backup script works well, except when it comes across the occasional file that has Chinese characters in the file name (some of my music files). I believe file names in NTFS are UTF-16 encoded (16-bit). PHP, which is designed to work with 8-bit character data, does not handle these file names well. It interprets these file names as gibberish (mostly question marks) and generates an error message that says the file does not exist. Then, of course, it is unable to copy these files.<br /><br />Does anybody know of a way to have PHP read a 16-bit file name correctly and then copy the file to another drive?<br /><br />Thanks!<!--content-->
Why don't you just use xcopy to make your backup?<!--content-->
Why not use the Backup Program that comes with XP?<br /><br />h_tp://www.microsoft.com/windowsxp/using/setup/learnmore/bott_03july14.mspx<!--content-->
Bruce and Madmanmcp,<br /><br />Thanks for your excellent suggestions. xcopy and the Windows XP Backup executable are both included with the Windows XP operating system and are logical choices for doing efficient, regularly scheduled backups.<br /><br />I decided to write a PHP script to back up files because I need to back up files, but also because I'm trying to use PHP wherever possible these days to improve my skills (and to see what the language has to offer). What I have discovered is that PHP has a history of weakness in the character-encoding area (see, for example, what Joel Spolsky <a href="http://www.joelonsoftware.com/articles/Unicode.html" target="_blank">has to say</a>).<br /><br />When I did not get an answer to my question about multibyte character encoding for file names at several different PHP user group forums, I contacted the PHP development team. From them, I learned that "readdir" is unable to read multibyte character encodings. The development team has gotten several requests for this feature and plans to add it in PHP 6.<br /><br />So for now, PHP's directory functions can only handle file names whose characters are represented in an ISO-8859-1 compatible encoding. This makes PHP unsuitable for backing up an NTFS-formatted drive, which uses UTF-16LE (2-byte) character encoding for all file names.<br /><br />(The exception is the special case where all files on an NTFS drive have names whose characters are included in the ISO-8859-1 repertoire; in this case, a PHP backup will work. This is because all information about each character is included in the first byte of its two-byte encoding, and the second byte is the null byte, which has no impact on the filename string in PHP. But requiring that all files have ISO-8859-1 compatible names seems unnecessarily restrictive when comprehensive backup utilities like xcopy and the Windows XP Backup executable exist.)<br /><br />By the way, for anyone interested in digging into the details of character encoding, Jukka Korpela has published <a href="http://www.cs.tut.fi/~jkorpela/chars.html" target="_blank">an informative primer</a>.<!--content-->
Good to know, thanks for the info.<br /><br />I have a problem with trying to reinvent the wheel and thus the suggestion. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /><!--content-->
 
Back
Top