abubaker0000 Posted October 15, 2014 Share Posted October 15, 2014 I used the move_uploaded_file function to upload files to my server ,but the function changes the Arabic names of files because most of my files are Arabic named how can I fix that ? Quote Link to comment https://forums.phpfreaks.com/topic/291634-move_uploaded_file-function-change-the-file-name/ Share on other sites More sharing options...
requinix Posted October 15, 2014 Share Posted October 15, 2014 (edited) Windows and PHP have never gotten along when it comes to non-ASCII filenames. PHP is not a Unicode program (as far as Windows knows) so it gets special treatment when it comes to filenames, but that special treatment can be a big problem. The best solution I know is this class. It requires the com_dotnet extension so if you cannot load that then you're out of luck. You use it like this: // say you want to write to a file named "C:\العربية.ext" $filename = "C:\\العربية.ext"; // first you need to make sure the filename is UTF-8 encoded. in your case it probably is // if not then you can use $filename = mb_convert_encoding($filename, "UTF-8", "original character encoding"); // now you detect windows systems. this is good for portability if (strncasecmp(PHP_OS, "WIN", 3) == 0) { // WindowsStreamWrapper requires the com_dotnet extension if (extension_loaded("com_dotnet") || function_exists("dl") && dl("com_dotnet")) { // if you do not have autoloading, //require_once("path/to/Patchwork/Utf8/WindowsStreamWrapper.php"); // while you can use the class normally, it is easier to use it as a stream wrapper // you can use any label for the stream wrapper name. I chose "winfs" stream_wrapper_register("winfs", "Patchwork\\Utf8\\WindowsStreamWrapper"); $filename = "winfs://" . $filename; } // if we cannot load the extension, ASCII filenames are safe and we don't have to worry about them // this detects bad filenames by looking for high bytes: 127-255 else if (preg_match('/[\x7F-\xFF]/', $filename)) { // there is no good solution. you may decide to raise a fatal (!) error //trigger_error("The com_dotnet extension is required for non-ASCII filenames but cannot be loaded", E_USER_ERROR); //exit; // or you can substitute a different filename and let the code continue // this is better in practice because a file with the wrong name is better than no file at all $filename = dirname($filename) . "\\" . uniqid(time(), true) . "." . pathinfo($filename, PATHINFO_EXTENSION); // (be careful: the file extension may not be ASCII either! in this code I assume it is) } } // now you can write to the file the same way you normally would. for example, file uploads still use move_uploaded_file(): move_uploaded_file($_FILES["foo"]["tmp_name"], $filename); Edited October 15, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/291634-move_uploaded_file-function-change-the-file-name/#findComment-1493645 Share on other sites More sharing options...
abubaker0000 Posted October 16, 2014 Author Share Posted October 16, 2014 $filename = "C:\\العربية.ext"; there are many files ... so that may get same name? Quote Link to comment https://forums.phpfreaks.com/topic/291634-move_uploaded_file-function-change-the-file-name/#findComment-1493707 Share on other sites More sharing options...
requinix Posted October 16, 2014 Share Posted October 16, 2014 $filename is an example. You have to change it according to your code. Quote Link to comment https://forums.phpfreaks.com/topic/291634-move_uploaded_file-function-change-the-file-name/#findComment-1493921 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.