[英]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);
我终于解决了错误。 这是我执行的步骤以及之间的错误消息:
flag.txt
检查了错误日志,以查找“ Permission Denied”错误。 flag.txt
文件的路径从相对于absolute更改为 。 事实证明,至少在我看来,cronjobs需要所有路径都是绝对的。 emails.php
文件将其更改为include_once()并成功了。 TL; DR:通过提供包含的所有文件的绝对路径来解决该错误。 同样,将include()语句更改为include_once()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.