Related to a student enrollment website, I have a PHP file that runs at 5:00 AM every morning to check for the students who haven't paid the fees. If the student hasn't paid the fees, they are dropped from the class. This file is called dropstudent.php
and this is the folder hierarchy:
cronjobs/
|
|- dropstudent.php
|- flag.txt
The flag.txt
file contains just one word - either true or false . Students can only pay the fees (which is handled in a PHP file elsewhere on the website) if the flag is set to true.
Now, before we drop the students, we set the flag to false, perform the checks and perform the necessary drops. In the end, we set this flag back to true.
So far, this was working just fine and no one touched this code. But recently, we have been having complaints about students not being able to pay. I looked into it and it turns out that the step where we are resetting the flag to true is not working since the flag gets changed to false but doesn't return to true.
Here is my code:
#!/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);
It would be great if someone can tell me how to figure out what is wrong here, and what can I do to fix it. I can post more detailed code (the checks and drop) if needed.
You need to call fclose($myfile);
after you do the first fwrite()
, to ensure that the buffer is flushed. The way you've written it, the first stream won't be closed until the script ends. At that time it will flush its buffer, overwriting what was written by the second stream.
It would be simpler if you used file_put_contents()
. It opens, writes, and closes the file in one step:
#!/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);
I finally resolved the error. Here are the steps I followed and the error messages I received in between:
flag.txt
. flag.txt
file from being relative to absolute . Turns out that cronjobs need all paths to be absolute, at least in my case. emails.php
file that I was including, I changed it to include_once() and it worked. TL;DR: Resolved the error by giving the absolute paths to all the files being included. Also, changed the include() statement to include_once().
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.