Page 1 of 1

Daily file deletion, and recreation. Error in the logic.

Posted: Sun Oct 01, 2006 2:23 am
by akimm
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:

Code: Select all

(!file_exists()) {
however, like I said, it brings back an error.

Code: Select all

<?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);

			?>

Posted: Sun Oct 01, 2006 2:50 am
by miro_igov
what is that strange piece of code you use:

Code: Select all

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;
                       }
If you want to write a file please refer the

Code: Select all

fwrite()

Posted: Sun Oct 01, 2006 3:00 am
by akimm
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.

Posted: Sun Oct 01, 2006 6:39 am
by Weirdan
  • to delete the file use unlink()
  • you don't need to scan the entire directory to check the ctime of one particular file
  • check out the manual pages on user-defined functions. It's what you attempted with $create_it part, but you done it wrong

Posted: Sun Oct 01, 2006 7:27 am
by akimm
ok weirden.

Thanks
:lol:

Posted: Sun Oct 01, 2006 8:49 am
by akimm
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.

Posted: Sun Oct 01, 2006 9:08 am
by Weirdan

Posted: Sun Oct 01, 2006 9:17 am
by akimm
Thanks.

Posted: Sun Oct 01, 2006 9:19 am
by akimm
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. ^_^

here is the function

Posted: Sun Oct 01, 2006 9:56 am
by akimm
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



here is my code as is now:

Code: Select all

<?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();
}
?>

Posted: Sun Oct 01, 2006 10:42 am
by Weirdan
akimm, could you translate this simple algorithm to php:
  1. get the creation time of the file, store it to $fctime variable
  2. get the current time, store it to $now variable
  3. if $fctime <= $now - (60*60*24), then
?

no need for functions, it seems user-defined functions is over your head yet.

Posted: Sun Oct 01, 2006 11:00 am
by akimm
Obviously so hehe. I did really try though.

Posted: Sun Oct 01, 2006 11:11 am
by akimm
Oh crap, I said unset, I meant for that to be unlink too. Crap.

Posted: Sun Oct 01, 2006 11:20 am
by akimm
ok here is the code.

I think now it works, it created the file, now I need to wait a day and see if it deletes and recreates.

Thanks for the help Weirdon.

Code: Select all

<?php
$now = date('F j, Y, g:i a');
$file = "iplog/ip.txt";
$creation_time = filectime($file);

if(!file_exists('iplog/ip.txt')) {
fopen('iplog/ip.txt', 'a');
if($creation_time <= $now - (60*60*24)) {
} else {
	unlink($file);
	fopen('iplog/ip.txt', 'a');
		}
	}
#i didn't use fclose, because I wasn't so sure I needed to , since fopen is only creating
?>