0

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

<?
$file = 'config/config2.php';

if(!empty($file))
{
some code here!
}
else
{
some other code here!
}
?>
4

2 回答 2

8
<?
$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!
}
?>
于 2013-09-05T21:07:54.313 回答
-1

The problem seems to be that you are not actually loading the file before you check if it is empty.

You are only setting the variable $file to the string 'config/config2.php' not actually loading the file.

Before running your if statement do this:

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

or look into this: http://php.net/manual/en/function.fopen.php

于 2013-09-05T21:21:50.650 回答