3

I have the below PHP script

<?php
header('Content-Type: text/html; charset=utf-8');
$file = "//192.168.10.206/wwwroot/SABIS CORPORATE/PrepList/Documents/41/1516 Level I Arabic Basic Questions and Answers T1 الأسلوب الخبري.pdf"; 
echo $file; 
?>

and the result is always

PS C:\Users\aaoun> php -q c:\Users\aaoun\Desktop\Test-AAA.php
//192.168.10.206/wwwroot/SABIS CORPORATE/PrepList/Documents/41/1516 Level I Arabic Basic Questions and Answers T1 ╪د┘╪ث
╪│┘┘ê╪ذ ╪د┘╪«╪ذ╪▒┘è.pdf PS C:\Users\aaoun>

I tried UT8 encoding

iconv("unicode", "utf-8", $file);    
iconv("Latin1_General_CI_AS","utf-8",$file); 
iconv("Arabic_CI_AS","utf-8",$file); 
base64_encode($file); 
utf8_encode($file);     
base64_decode($file); 
utf8_decode($file);  
iconv('WINDOWS-1256', 'UTF-8', $file);   
iconv('cp1256', 'UTF-8', $file);

Nothing seems to work, I keep getting wrong text I need to get the text to check if the file exists ...

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
Anthony
  • 31
  • 1
  • 9
  • `echo file_exists("your_filename");` – NSNoob Dec 14 '15 at 11:57
  • / / 1 9 2 . 1 6 8 . 1 0 . 2 0 6 / w w w r o o t / S A B I S C O R P O R A T E / P r e p L i s t / D o c u m e n t s / 4 1 / 1 5 1 6 L e v e l I A r a b i c B a s i c Q u e s t i o n s a n d A n s w e r s T 1 ♠'♠D♠#♠3♠D♠H ♠( ♠'♠D♠.♠(♠1♠J . p d fPHP Warning: file_exists() expects parameter 1 to be a valid path, string given in C:\Users\aao un\Desktop\Test-AAA.php on line 6 – Anthony Dec 14 '15 at 12:56

1 Answers1

0

Ensure that the encoding of the file-system is the same as the encoding of the string that contains the file-name in your PHP code.

Otherwise you're testing for the wrong file to exist.

For example, if your file-system does not support UTF-8 but the file-name is encoded in UTF-8. As you are using Windows, it is worth noting that Windows filesystems (FAT, FAT32, NTFS) are not UTF-8 aware. you can convert your file name to UTF-16 like

$newName = mb_convert_encoding('your file path', 'UTF-16', 'UTF-8');

If that does not work, you can use urlencode. All characters returned from urlencode are valid in filenames (NTFS/HFS/UNIX).

$newName = urlencode($file);

Try this code to know if the file exists:

if (file_exists('your file path')) {   
//file exists;                         
}
else
{
//Does not exist
}
NSNoob
  • 5,548
  • 6
  • 41
  • 54