使用文本文档文件系统,检查操作不起作用?

时间:2016-06-30 22:55:02

标签: php

大家好我有一个简单的系统我用来保存用户文档数据的基本数据库。我知道这根本不是传统方法,但我想知道它是否有用。到现在为止还挺好。但是,我已经找到了一个问题,我的网站正在输出我的echo标签中的数据作为正确的语句,以便在加载页面时触发我的if命令但由于某种原因它不会更改文本文档中的信息。有谁知道我在这里可能会缺少什么?

<?php
ob_clean();session_start();

if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}

if (isset($_GET['Username'])){  
    $Username = $_GET['Username'];
}

$myFile=fopen("USERS/".$Username."/NoOfDocuments.txt","r") or exit("Can't open file!");
$NoOfDocs = fgets($myFile);
fclose($myFile);

$myFile=fopen("USERS/".$Username."/RiskAssessment/Subscribed.txt","r") or exit("Can't open file!");
$Subscribed = fgets($myFile);
fclose($myFile);

$myFile=fopen("USERS/".$Username."/DOC1/Name.txt","r") or exit("Can't open file!");
$DocName = fgets($myFile);
fclose($myFile);

$myFile=fopen("USERS/".$Username."/DOC1/URL.txt","r") or exit("Can't open file!");
$DocURL = fgets($myFile);
fclose($myFile);

if ($Subscribed == false){
    if ($NoOfDocs == 0){
        $myFile=fopen("USERS/".$Username."/NoOfDocuments.txt","w") or exit("Can’t open file!");
        fwrite($myFile, ($NoOfDocs +1));
        fclose($myFile);

        $myFile=fopen("USERS/".$Username."/DOC1/Name.txt","w") or exit("Can’t open file!");
        fwrite($myFile, 'Risk Assessment');
        fclose($myFile);

        $myFile=fopen("USERS/".$Username."/DOC1/URL.txt","w") or exit("Can’t open file!");
        fwrite($myFile, 'RiskAssessment.php');
        fclose($myFile);

        $myFile=fopen("USERS/".$Username."/RiskAssessment/Subscribed.txt","w") or exit("Can’t open file!");
        fwrite($myFile, 'true');
        fclose($myFile);
    }       
}

echo 'Logged in as = '.$Username.'<br>';
echo 'Document Subscribed = '.$Subscribed.'<br>';
echo 'Number of Documents Subscribed to = '.$NoOfDocs.'<br>';
echo 'This Document is Called = '.$DocName.'<br>';
echo 'This Document is Located at = '.$DocURL.'<br>';

?>

编辑1.0

这是我的echo标签按要求输出的信息:

Logged in as = Matt
Document Subscribed = false
Number of Documents Subscribed to = 0
This Document is Called = DOC1-NAME
This Document is Located at = DOC1-URL

编辑1.1

使用Barmar方法,仍然得到相同的结果:

<?php
ob_clean();session_start();

if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}

if (isset($_GET['Username'])){  
    $Username = $_GET['Username'];
}

$NoOfDocs = file_get_contents("USERS/".$Username."/NoOfDocuments.txt") or exit("Can't read file!");
$Subscribed = file_get_contents("USERS/".$Username."/RiskAssessment/Subscribed.txt") or exit("Can't read file!");

if ($Subscribed == 'false'){
    if ($NoOfDocs == 0){    
        file_put_contents("USERS/".$Username."/NoOfDocuments.txt", ($NoOfDocs +1)) or exit("Can't write file!");
        file_put_contents("USERS/".$Username."/DOC1/Name.txt", "Risk Assessment") or exit("Can't write file!");
        file_put_contents("USERS/".$Username."/DOC1/URL.txt", "RiskAssessment.php") or exit("Can't write file!");
        file_put_contents("USERS/".$Username."/RiskAssessment/Subscribed.txt", "true") or exit("Can't write file!");
    }       
}
?>

1 个答案:

答案 0 :(得分:1)

$Subscribed是一个字符串,而不是布尔值。 fgets()只有在收到错误(例如文件结束)时才返回布尔值false。它应该是:

if ($Subscribed == 'false') {

此外,如果文件的行尾有换行符,则fgets()将返回这些换行符。你应该减掉它:

if (trim($Subscribed) == 'false') {

顺便说一句,由于这些文件只有一行,您可以使用file_get_contents一步阅读:

$Subscribed = file_get_contents("USERS/".$Username."/RiskAssessment/Subscribed.txt") or exit("Can't read file!");

然后将其写回:

file_put_contents("USERS/".$Username."/RiskAssessment/Subscribed.txt", "true") or exit("Can't write file!");