PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
This code is meant to check to see if ip.txt is more than a day old, if so I want it deleted then recreated. This is for a unique counter I'll be employing on my webbie once I get this done. I'm not sure why, it says error dividing by zero,since the file doesn't exist, yet I thought I instructed it to create the file with this bit:
<?php
#path to the file which I want to use
$path = "iplog/";
#code to create my file in the case that it was deleted or never created
$create_it = fopen($path . 'ip.txt', 'a');
#make sure path is indeed a directory.
if(is_dir($path)) {
#handle to open that path
$handle = opendir($path);
#read the dir if so
while ($file = readdir($handle)) {
#find that file now!!!
if (is_file($path.$file)) {
#time comparison, to eventually check if this file has exists 1 day!
$diff = round((time() - filectime($path.$file))/3600/24);
#if it has exists KILL THE BASTARD!!!!!
if ($diff < 1) {
#AHHH file died : (
unset($file);
#make sure he died, shoot him twice in the head, Godfather 1 style, make sure they're dead
if(!file_exists($file)){
#ahh, thank God for resurrection, my file lives again
$create_it;
#in the event that the file was never made : (, create it!
} else {
$create_it;
}
}
}
}
}
fclose($create_it);
?>
if(!file_exists($file)){
#ahh, thank God for resurrection, my file lives again Smile
$create_it;
#in the event that the file was never made : (, create it!
} else {
$create_it;
}
I know how to write to a file. One of the only things I do actually know.
That strange code is as the comments might suggest. I don't want it to writeanything to the code. ITs meant to evaluate the ip.txt age. IF the age is over 1 day, it deletes it, after the deletiton it then recreates ip.txt so it can be reused for another day.
Weirden, I've looked up user-defined functions on php.net, it didn't recognize that exactly,is there one specific function i'm searching for. It brought up a bunch of results, but nothing definitive.
Oh, you had me confused, so you think writing a function for this will make it much easier? I thought there was some pre-written function that would automate tasks, sort of a cron deal. I mistook you obviously, but i'll try my hand at a function. ^_^
Ok, here is the funciton, but I think I've fallen into the same pitful. I tried different functionallity, then tried as weirden suggested, using a function. Still I went wrong, my error is:
Warning: Division by zero in /nfs/cust/8/25/05/650528/web/journal06/journal.php on line 323
<?php
function checkfile() {
$fp = fopen('iplogs/ip.txt', 'a');
$file = "iplogs/ip.txt";
$diff = round((time() - fstat($file))/3600/24);
// tis a young file, let him live!
while($dif < 1) {
// break the death day for $file
break;
// re-establish death day for file, he lived his time, like a fly he has a short life span :-/ }
while ($dif >= 1) {
// delete the old file, its filesystem eugenics
unset($file);
// break loop after unsetting
break;
}
// if for some reason, the file never existed, create that sucker!
if(!file_exists($file)) {
// I just want it to use file pointer to establish the file, not actually write any content
fwrite($fp);
} else {
// do nothing because the file exists and its less than one day: -)
}
}
// close file
fclose($fp);
// return function
return checkfile();
}
?>