繁体   English   中英

当PHP无法更改文件中的标志时,如何分辨我的网站上的错误?

[英]How to tell what the error is on my website when PHP can't change flag in a file?

与一个学生注册网站有关,我有一个PHP文件,该文件每天早上5:00 AM运行,以检查未付费用的学生。 如果学生未支付费用,则将其从班级中删除。 该文件称为dropstudent.php ,这是文件夹层次结构:

cronjobs/
|
|- dropstudent.php
|- flag.txt

flag.txt文件只包含一个字- 如果标志设置为true,则学生只能支付费用(在网站上其他位置的PHP文件中处理)。

现在,在丢弃学生之前,我们将标志设置为false,执行检查并执行必要的丢弃操作。 最后,我们将此标志设置回true。

到目前为止,这还算不错,没有人碰过这段代码。 但是最近,我们一直抱怨学生无法付款。 我调查了一下,结果发现我们将标志重置为true的步骤不起作用,因为标志已更改为false但不会返回true。

这是我的代码:

#!/opt/rh/php55/root/usr/bin/php

<?php

// require '../dbmanage.php';
require '/path to college servers/dbmanage.php';

logCronJob("dropstudent", "begin");

$filepath = '/path to college servers/cronjobs/flag.txt';

// Change flag to false
$myfile = fopen($filepath, "w") or die("Unable to open file!");
$txt = "false";
fwrite($myfile, $txt);

// do the drop checks and other relevant stuff here

// Change flag back to true
$myfile = fopen($filepath, "w") or die("Unable to open file!");
$txt = "true";
fwrite($myfile, $txt);
fclose($myfile);

如果有人能告诉我如何弄清楚这里出了什么问题以及我该怎么做才能解决,那将是很好的。 如果需要,我可以发布更详细的代码(检查和删除)。

您需要调用fclose($myfile); 在执行第一个fwrite() ,以确保刷新了缓冲区。 按照您的编写方式,直到脚本结束,才会关闭第一个流。 那时它将刷新其缓冲区,覆盖第二个流写入的内容。

如果使用file_put_contents()会更简单。 它一步打开,写入和关闭文件:

#!/opt/rh/php55/root/usr/bin/php

<?php

// require '../dbmanage.php';
require '/path to college servers/dbmanage.php';

logCronJob("dropstudent", "begin");

$filepath = '/path to college servers/cronjobs/flag.txt';

// Change flag to false
file_put_contents("false", $filepath);

// do the drop checks and other relevant stuff here

// Change flag back to true
file_put_contents("true", $filepath);

我终于解决了错误。 这是我执行的步骤以及之间的错误消息:

  1. 正如@SulthanAllaudeen所建议的那样,我在打开flag.txt检查了错误日志,以查找“ Permission Denied”错误。
  2. 向主管部门咨询了权限或系统升级方面的最新更改。 他们没有做任何改变。
  3. 遵循@Barmar的建议关闭文件句柄。
  4. 仍然不断收到相同的错误。
  5. 然后将flag.txt文件的路径从相对于absolute更改为 事实证明,至少在我看来,cronjobs需要所有路径都是绝对的。
  6. 这解决了“权限被拒绝”错误。 但是后来我开始出现错误“无法重新声明该功能”。 因此,我所包含的emails.php文件将其更改为include_once()并成功了。
  7. 我仍然按照@Barmar的建议关闭文件句柄,这是一个好习惯。

TL; DR:通过提供包含的所有文件的绝对路径来解决该错误。 同样,将include()语句更改为include_once()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM