Ok I am working on making a auto delete function for a shoutbox script that uses a text file for the database. The function should delete any data that is greater than a set number. This is what I have now.
This seems to work fine for a bit, but then starts to slowly remove parts of the last $key.PHP Code:function deleteOld() {
global $entriesFile;
$limit = "10"; // Any key greater than 10 will be deleted
$shout = file ($entriesFile);
while (list ($key, $val) = each ($shout)) {
if ($key >= $limit) {
list($id, $time, $shoutbox_name, $shoutbox_msg, $shoutbox_url, $shoutbox_ip) = explode("|",$val);
$fp = fopen($entriesFile, "r");
$data = fread($fp, filesize($entriesFile));
fclose($fp);
$old = "$id|$time|$shoutbox_name|$shoutbox_msg|$shoutbox_url|$shoutbox_ip|";
$new = "";
$data = str_replace($old, $new, $data);
$fp = fopen($entriesFile, "w");
fputs($fp, $data);
fclose($fp);
}
}
}
example of the text file:
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shoutbox_ip|
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shoutbox_ip|
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shoutbox_ip|
What starts to happen:
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shoutbox_ip|
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shoutbox_ip|
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shoutbox_
Then:
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shoutbox_ip|
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shoutbox_ip|
id|time|shoutbox_name|shoutbox_msg|shoutbox_url|shou
I can't figure out why it is removing sections of the last $key like that. It seems to be random also, does it sometimes doesn't do it other times. Has anybody had a problem like this before? Or have any ideas of whats going on? Thanks.


Reply With Quote
Bookmarks