提问者:小点点

为什么文件获取内容在PHP中不起作用?


此代码获取内容文件txt:

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

文件存在于文件夹中:

结果:显示错误:

警告:file_get_contents(c:\表\t.txt):未能打开流:第6行的C:\xampp\htdocs\方向\test.php中没有这样的文件或目录

为什么file\u get\u contents()不起作用?

如何读取路径c的内容:\表\t、 txt?


共3个答案

匿名用户

尝试使用urlencode对您的目录名进行编码:

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

这里对此进行详细描述。

编辑

假设您使用的是UTF-8编码的源文件,您也可以尝试以下方法之一

$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';

据我所知(

编辑2

您还可以尝试将UTF-8文件名转换为不同的编码,例如SJISSJIS-winSJISJISEUC-JPEUC-JP-winCP932JIS-ms等。但我不是东亚字符编码方面的专家,所以请谨慎对待这些信息。

匿名用户

试试这个

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

echo $con;
?>

匿名用户

语言: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.

被改进的:

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

解释:

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.

注意:如果您的文件夹和文件中仍然使用汉字,那么您必须将操作系统从Windows更改为ubuntu。