file_get_contents与空文件无法正常工作PHP [英] file_get_contents with empty file not working PHP
问题描述
我尝试使用file_get_contents形式的PHP,但它不工作。
有我的代码:
$ filename =/opt/gemel/test.txt;
$ b $ if($ filecontent = file_get_contents($ filename)){
$ nom = fgets(STDIN);
file_put_contents($ filename,$ nom,FILE_APPEND);
}
else
echofail;
我的文件 test.txt 是空的。 (0个八位字节)。他存在,但他是空的。
当我写入东西,我的代码完美的作品,但如果他是空的我的代码回声失败
$ b
函数 file_get_contents
返回文件中的字符串。如果该文件不包含数据,则 file_get_contents
将返回一个空字符串。如果你想尝试 var_dump(''== false);
,你会得到 true
。所以,即使文件可以被读取,文件的内容也会是 false
。
如果你愿意($ filecontent = file_get_contents($ filename)!== false){
$ b
if b $ b
编辑;链接到比较运算符的文档。
I try to use file_get_contents form PHP but it's not working.
There is my code :
$filename = "/opt/gemel/test.txt";
if($filecontent = file_get_contents($filename)){
$nom = fgets(STDIN);
file_put_contents($filename, $nom, FILE_APPEND);
}
else
echo "fail";
And my file test.txt is empty. (0 octets). He exists but he is empty.
When i write something into it, my code works perfectly but if he is empty my code echo "fails"
Why that, why he can't open the file text.txt ?
The function file_get_contents
returns the string that's in the file. If the file contains no data, then file_get_contents
returns an empty string. If you would try to var_dump('' == false);
you would get true
. So, even though the file can be read, the contents of the file evaluates to false
.
If you would use this line, your code should work:
if($filecontent = file_get_contents($filename) !== false){
Edit; link to the documentation of the Comparison operators.
这篇关于file_get_contents与空文件无法正常工作PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!