使用 fopen 和 fread 时出现语法错误


Getting syntax error while using fopen and fread

$file = "status.txt";
$open = fopen($file, "r");
$size = filesize($file);
$count = fread($open, $size);
if($count == 1) {
    header('Location: http://www.google.com/');
} else {
    echo "Status is unavailable";
}

您好,我正在尝试读取文本文件。
执行此操作时,我Parse error: syntax error, unexpected T_STRING收到错误。
我正在尝试读取状态.txt如果它有 1,它将重定向,否则它会说状态不可用。
有什么想法吗?

我必须指出,对于像这样的简单文件,file_get_contentsfopenfreadfclose(您省略)等要容易得多。

您发布的代码本身似乎没有任何问题。也许你在整个街区之前搞砸了一些东西?尝试注释掉内容以隔离错误。这是您需要获得的调试技能。

错误肯定不在这些行中! 请包括整个PHP脚本,在这些行之前或之后一定有问题。

请尝试此代码!

<?php
    $file = 'status.txt';
    $contents = file_get_contents($file);
    if ((strlen($contents)-1) == 1) {
        header('Location: http://www.google.com');
    } else {
        echo 'Status is unavailable';
    }
?>

压缩中的 (-1),因为最后一行的新行字符计数。

如果您谈论的是"status.txt"的内容,请在引号之间加上"1"。 如果它只是一个 TRUE 或 FALSE 语句,只需使用 if($count){ ... }否则{ ...}