3

Hello I have a problem in my PHP code : I need to open a txt file using file_get_contents, the directory to the txt file contains arabic Characters my code:

$URLX = "C:/server files/BN/public_html" ; 
$BookName = "الإضافةإلىمفاوضةمالكحسون" ; 
$eachChapiter[$x] = "مالكمالكحسون" ; 

$content = file_get_contents($URLX."/".$BookName."/".str_replace(' ', '', $eachChapiter[$x]).".txt"); 

the error output :

WARNING: FILE_GET_CONTENTS(C:/SERVER FILES/BN/PUBLIC_HTML/الإضافةإلىمفاوضةمالكحسون/مالكمالكحسون.TXT): FAILED TO OPEN STREAM: NO SUCH FILE OR DIRECTORY IN C:\XAMPP\HTDOCS\LIBRARY\SEARCH\SEARCH.PHP ON LINE 30

NOTICE: UNDEFINED OFFSET: 1 IN C:\XAMPP\HTDOCS\LIBRARY\SEARCH\SEARCH.PHP ON LINE 38
Suh Fangmbeng
  • 573
  • 4
  • 16
Rami Osman
  • 137
  • 1
  • 13
  • 1
    php isn't unicode aware. you can't use php's standard string functions on unicode text without destroying the text. use the `mb_*()` functions instead, which ARE unicode-aware – Marc B Jul 19 '16 at 15:27
  • 1
    According to [this question and answer](http://stackoverflow.com/questions/708017/can-a-php-file-name-or-a-dir-in-its-full-path-have-utf-8-characters), PHP's file functions can't open any files that have paths with characters outside ISO-8859-1. Arabic is not part of that encoding, so it's not possible. The `mb_*()` family of functions will not help you here. –  Jul 19 '16 at 15:28

1 Answers1

1

Thanks @Mike for helping me correct the answer. So As per the question and OP's OS it seems that you(OP) are encountering a variant of this error.

And as discussed on that link, you can use the following code to detect the file name encoding

...    

$final_url = $URLX."/".$BookName."/".str_replace(' ', '', $eachChapiter[$x]);

mb_detect_encoding($final_url, 'UTF-8', true)) ? utf8_decode($final_url) : $final_url;
$content = file_get_contents($final_url).".txt");

Remember that this code will create problems(again discussed on that link) on a linux server. So if you are using Linux production environment then you can apply the solution suggested there.

Community
  • 1
  • 1
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
  • Hmmm... not sure if this applies in *this* case, where the problem appears to have to do with an Arabic *file name.* It sounds to me like the other answer might be closer to the mark in *this* case, although I agree that your answer might be proper in *other* cases where multinational-encoded strings are being used. – Mike Robinson Jul 19 '16 at 15:32
  • Are you using windows OS? – Mohd Abdul Mujib Jul 19 '16 at 15:33
  • Suggest that you edit your answer one more time: "Are you using Windows OS?" It sure does appear to me that he *is.* So, edit your answer to tell us just *what* you're thinking might be relevant, "assuming" that it is, indeed, Windows. **:-)** *"Are you using Windows OS? Because, if so ..."* (etc.) – Mike Robinson Jul 19 '16 at 15:35
  • @MikeRobinson yup :D I missed the file directory location :) – Mohd Abdul Mujib Jul 19 '16 at 15:36
  • I have always resolved that I wont provide answers when high on ****, but I always break that. #addicted_to_so :D – Mohd Abdul Mujib Jul 19 '16 at 15:43
  • Hey, I didn't downvote you! Quite the opposite. **:-)** – Mike Robinson Jul 19 '16 at 15:50
  • thanks guys for your reply , I'm using Windows Server 2012. – Rami Osman Jul 19 '16 at 16:38