PHP-在单独的目录中引用和追加一个txt日志文件


PHP- Referencing and appending to a txt log file in separate directory

和我们所有人一样,我正在冒险进入另一个编程冒险。我正在用xhtml标准1.0、CSS、Javascript和PHP构建一个网站。这里没什么特别的,但是我在PHP开发过程中遇到了一个非常有趣的问题。

我写了工作代码发送电子邮件到网站联系电子邮件,并通过表单发送。我还想在服务器内部记录这些事务。根据我的研究,这应该很容易。

    $fileVar = fopen("../data/feedback.txt", "a")
    or die("Error: Could not open the log file.");
    fwrite($fileVar, "'n-------------------------------------------------------'n")
    or die("Error: Could not write to the log file.");
    fwrite($fileVar, "Date received: ".date("jS 'of F, Y 'a''t H:i:s'n"))
    or die("Error: Could not write to the log file.");
    fwrite($fileVar, $messageToBusiness)
    or die("Error: Could not write to the log file.");

我存储反馈文本的目录将是我的public_html(主目录,然后是easy easy…data/feedback.txt.

我正在使用标准权限…实际上没有什么特别的,755表示目录,644表示文件。然而,每次执行时,我都会收到第一个错误。

("错误:无法打开日志文件。")

我需要帮助,更重要的是,如果你知道为什么,你能给我一个简短的解释,或者如果你没有时间,你能稍后提供一个资源链接吗?我似乎不明白这是怎么回事。

谢谢你的阅读,如果我先找到答案,我会贴出来的。

编辑:我已经决定包括整个代码,希望错误源于上面。我将感谢任何信息和建设性的反馈。我不是唯一一个这样做的人,所以我的问题集中在为其他人提供资源上。

<?php
//SendEmail.php
$messageToBusiness = 
    "From: ".$_POST['firstname']." "
            .$_POST['lastname']."'r'n" .
    "E-mail address: ".$_POST['email']."'r'n".
    "Phone number: ".$_POST['phone']."'r'n".
    "Subject: ".$_POST['Please_Choose']."'r'n".
    "Message Text: 'r'n".$_POST['Message']."'r'n";
$headerToBusiness = "From: $_POST[email]'r'n";
mail("emailreplacement@gmail.com", $_POST['subject'], $messageToBusiness, $headerToBusiness);
$messageToClient =
    "Dear " .$_POST['lastname'].":'r'n".
    "The following message was received from you by website:'r'n'r'n".
    $messageToBusiness.
    "------------------------'r'nThank you for the taking the time to contact us, our representatives will respond as soon as we have the appropriate information for you. 
    Thank you for your patronage.'r'n" .
    "Business Rep 'r'n------------------------'r'n";
if ($_POST['reply'])
    $messageToClient .= "Please feel free to contact us with any more concerns you may have!";
$headerToClient = "From: fakeemail@fake.com'r'n";
mail($_POST['email'], "Re: ".$_POST['subject'], $messageToClient, $headerToClient);
$display = str_replace("'r'n", "<br />'r'n", $messageToClient);
$display =
    "<html><head><title>Your Message</title></head><body><tt>".
    $display.
    "</tt></body></html>";
echo $display;
$fileVar =fopen("../data/feedback.txt", "a")
    or die("Error: Could not open the log file.");
fwrite($fileVar, "'n-------------------------------------------------------'n")
    or die("Error: Could not write to the log file.");
fwrite($fileVar, "Date received: ".date("jS 'of F, Y 'a''t H:i:s'n"))
    or die("Error: Could not write to the log file.");
fwrite($fileVar, $messageToBusiness)
    or die("Error: Could not write to the log file.");
?>

漏接/:

$fileVar = fopen("..data/feedback.txt", "a")
                    ^--- here

应为../data/feedback.txt。如果没有额外的斜杠,您将尝试在当前目录中使用名为..data的目录。由于该子目录与您存在的子目录不同,因此您无法在其中打开文件,因此出现"cannot open file"错误。