3

This code get content file txt:

$str = "c:\\\\表\\t.txt";
$con=file_get_contents( $str);
echo $con;

File exist in folder:

enter image description here

Result: show error:

Warning: file_get_contents(c:\表\t.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 6

Why is file_get_contents() not working?

How read content of path c:\表\t.txt?

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
D T
  • 3,522
  • 7
  • 45
  • 89
  • Please have a look at http://stackoverflow.com/questions/10008341/list-directories-containing-unicode-characters-on-windows – Apb Jan 20 '16 at 08:50
  • 3
    That character is a bit problematic, there are issues converting it between Unicode and SJIS, see also http://stackoverflow.com/questions/34894581/php-error-with-character-%E8%A1%A8-jp#comment57526221_34894581 – Thilo Jan 20 '16 at 08:53
  • 1
    Use `var_dump(is_file("c:\\\\表\\t.txt"), is_readable("c:\\\\表\\t.txt")); ` to debug. – The Onin Jan 20 '16 at 09:52
  • Possible duplicate of [How do I use filesystem functions in PHP, using UTF-8 strings?](http://stackoverflow.com/questions/1525830/how-do-i-use-filesystem-functions-in-php-using-utf-8-strings) – Ferrybig Jan 20 '16 at 10:30

4 Answers4

2

Try to use urlencode to encode your directory name:

$str = 'c:\\'.urlencode('表').'\t.txt';
$con=file_get_contents( $str);
echo $con;

This is described here in detail.

EDIT

Assuming you're using UTF-8 encoded source files, you could also try one of the following

$str = 'c:\\'.urlencode(mb_convert_encoding('表', 'UTF-16', 'UTF-8')).'\t.txt';
// or just
$str = 'c:\\'.mb_convert_encoding('表', 'UTF-16', 'UTF-8').'\t.txt';

As far as I know newer (> FAT32) Microsoft filesystems use UTF-16 encoding. But this will make your solution fail on other (e.g. Linux) filesystems.

EDIT 2

You can also try to convert your UTF-8 filename into a different encoding such as SJIS, SJIS-win, SJIS-2004, JIS, EUC-JP, eucJP-win, EUC-JP-2004, CP932, JIS-ms or the like. But I'm not an expert in east asian character encodings - so treat that information with caution.

Community
  • 1
  • 1
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • Warning: file_get_contents(c:\%E8%A1%A8\t.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 6 – D T Jan 20 '16 at 10:40
  • What's the character encoding of your file? UTF-8? – Stefan Gehrig Jan 20 '16 at 10:44
  • my file is utf-8, it still show errror: Warning: : failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 6 – D T Jan 20 '16 at 10:50
  • i try change UTF-16 to all encode: SJIS, SJIS-win, SJIS-2004, JIS, EUC-JP, eucJP-win, EUC-JP-2004, CP932, JIS-ms, but it still not woking. – D T Jan 20 '16 at 11:34
  • @DT: Then I think you're out of look. If it's an option to change the name of the folder something containing only ASCII characters, the do this and you're done. – Stefan Gehrig Jan 20 '16 at 11:35
1

try this

<?PHP
$con=file_get_contents('./表/t.txt', true);
$con=file_get_contents('./表/t.txt',FILE_USE_INCLUDE_PATH);

echo $con;
?>
naseeba c
  • 1,040
  • 2
  • 14
  • 30
  • Warning: file_get_contents(C:/表/t.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 4 – D T Jan 20 '16 at 09:02
  • Warning: file_get_contents(./表/t.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 4 Warning: file_get_contents(./表/t.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 5 – D T Jan 20 '16 at 09:08
  • what is your corrent directory?? – naseeba c Jan 20 '16 at 09:15
  • you just try this `var_dump(getcwd());` – naseeba c Jan 20 '16 at 09:18
  • this my folder "C:\表" – D T Jan 20 '16 at 09:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101165/discussion-between-d-t-and-naseeba-c). – D T Jan 20 '16 at 09:19
  • so try this `file_get_contents('./t.txt'.true);` – naseeba c Jan 20 '16 at 09:20
1

Language : PHP

$results = scandir('c:\');
$result will give you all folder name inside c drive.
you can find your folder in $result array.
now you have folder name in $result and now you can go to file.

Modified :

$results = scandir('/web');
$results = "/web/$results[21]/t.txt";
$con=file_get_contents( $results);
echo $con;

Explanation :

1.) /web is the directory where is  表 folder and some other folders.
2.) $result[21] is giving me value 表 on browser i know its 表 folder.
3.) Now you have file path. Go ahead.

NOTE : If you still use Chinese character in your folder and file then you have to change your OS from window to ubuntu.

Monty
  • 1,110
  • 7
  • 15
  • result return folder 表 is string(2) "�\" – D T Jan 20 '16 at 10:00
  • If you getting folder name in $result[2] then use it in your way. $str = "C:\$results[2]\.t.txt"; $con=file_get_contents( $str); echo $con; – Monty Jan 20 '16 at 10:06
  • it sill show error : Warning: : failed to open stream: No such file or directory in – D T Jan 20 '16 at 10:15
  • do one thing. move your file in different drive or make a copy of that file and give permission to that file and apply your approach – Monty Jan 20 '16 at 10:21
  • i try move to htdoc, but still can't read file, this problem is folder name =表, if i set folder name ="a", it will read file ok. – D T Jan 20 '16 at 10:25
  • Do you set folder name =表? – D T Jan 20 '16 at 10:34
  • My URL: http://localhost/direction/test.php, C:\xampp\htdocs\direction\表, My code: $results = scandir('/direction'); =>error: Warning: scandir(./direction,./direction): The system cannot find the file specified. – D T Jan 20 '16 at 10:45
  • 1
    Please come in chat. i will explain you there. I have ubuntu OS – Monty Jan 20 '16 at 10:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101177/discussion-between-monty-and-d-t). – Monty Jan 20 '16 at 10:54
0

Create path based on single /

$content = file_get_contents("C:/表/t.txt");

You can read more about Filesystem here: http://php.net/manual/en/wrappers.file.php

Armen
  • 4,064
  • 2
  • 23
  • 40
  • Warning: file_get_contents(c:/表/t.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 3 – D T Jan 20 '16 at 08:47
  • Warning: file_get_contents(C%3A%2F%E8%A1%A8%2Ft.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\direction\test.php on line 4 – D T Jan 20 '16 at 08:56