如果文件为空,否则


IF file is !empty ELSE

所以,如果我有一个文件,当它为空时,它需要一个特定的功能,但如果文件不为空,它将继续显示(其他)其他内容。这是我目前使用的格式。我已经厌倦了几种不同的方式和变化(从PHP手册示例到StackOverFlow Q/A)。它正在做的是向我显示其他而不是如果,因为文件实际上是空的......

<?
$file = 'config/config2.php';
if(!empty($file))
{
some code here!
}
else
{
some other code here!
}
?>
<?
$file = 'config/config2.php';
if(filesize($file)!=0)// NB:an empty 'looking' file could have a file size above 0
{
some code here!
}
else
{
some other code here!
}
?>

问题似乎是您在检查文件是否为空之前实际上并没有加载文件。

您只是将变量$file设置为字符串"config/config2.php",而不是实际加载文件。

在运行 if 语句之前,请执行以下操作:

$file = file_get_contents('config/config2.php');

或者看看这个:http://php.net/manual/en/function.fopen.php