I have a program that has been working fine for a couple of years. It is a program that allows clients to update a flat file database file (not mysql) It reads in the file matches up what has been changed and writes out the file again. As I said it has been working perfectly for a couple of years no problems. Today somehow one of my clients managed to write out a 0byte data file using the program (presumeably I wasn't there don't exactly know as users aren't terribly detailed on what they did and what happened. Sort of we were doing some updating and it all disappeared)
Anyone have any clue what would cause such a thing to happen and how to prevent it?
Below is the subroutine that does the updating. Not sure how much good it will do anyone without the rest of the file but perhaps.
# update item
case'update':
{
if (isset($_SESSION['first_name'])) {
$site_id=$_SESSION['site_id'];
$site_name=$_SESSION['site_name'];
$site_opt=$_SESSION['site_opt'];
echo "<div id=\"Header\"><h1>$site_name website maintenance welcome";
echo ", {$_SESSION['first_name']}!";
echo '</h1></div><br><h4 align=center>Click on a button above to get a menu for that area of your website.</h4>';
}else {
echo '<br><br><br><br><br><br><h1> You must be logged in to view this page.</h1><h1><a class=log href="index.php">LOGIN</a></h1>';
include("./includes/footer.html");
die();
}
echo '<div id="Content">';
echo "<h4 align=center>$page_title -- Edit Item</h4>";
$config="shop/".$site_id."_config.php";
include("includes/menu.php");
include ("$config");
# check on empty inputs
$error = false;
$fld="";
$reqs = array('Item ID'=>'itemid','Short Description'=>'desc','Long Description'=>'description');
while (list($key,$val) = each($reqs)) {
if (!isset($_POST["$val"])|| isset($_POST["$val"]) && empty($_POST["$val"])){
$error = true;
$fld.="Fill in the ".$key." <br>";
}
if ($error == true)
{
echo "<p align=center><font size=4> Please correct the following errors.<br>".$key." </font>\n";
echo "<a href=javascript:history.back();><font size=4>GO Back</a></font></p>\n";
include("includes/footer.html");
break;
}
}
$d2=eregi_replace("[\n\r\t\n]",' ',$_POST['description']);
$d2=trim($d2);
$d2 = escape_data($d2);
$d = stripslashes($_POST['desc']);
$d=trim($d);
$price=eregi_replace('$',' ',$_POST['price']);
$price=stripslashes($price);
$price=trim($price);
$id=stripslashes($_GET['id']);
$id=trim($id);
$newid=stripslashes($_POST['itemid']);
$newid=trim($newid);
$photo=stripslashes($_POST['photo']);
$photo=trim($photo);
if (trim($_POST['opt'])!=''){
$opt="%%OPTION%%";
$opt=trim($opt);
$opt.=trim($_POST['opt']);
$opt=stripslashes($opt);
$opt=trim($opt);
$opt =eregi_replace('.html','',$opt);
$opt =eregi_replace('.htm','',$opt);
$opt.=".html";
}else{
$opt="%%OPTION%%blank.html";
}
$entry=$newid."|".$_POST['cat']."|".$price."|".$d."|<img src=\"".$photo."\">|".$d2."|".$opt."\n";
$data=fopen($shop_data_file,"r");
$x=0;
while (!feof($data)) {
$buffer[$x] = fgets($data);
$x++;
}
fclose($data);
#open data file and check for duplicate ids
$data=fopen($shop_data_file,"r");
foreach($buffer as $ke => $val){
#split items into fields
$itarray = explode('|', "$val");
if($itarray[0]==$newid && $id != $newid){
echo $itarray[0],' '.$newid.' '.$id.'<br>';
$err=duplicateid;
fclose($data);
break;
}
}
#open data file and update
if ($err != duplicateid){
$data=fopen($shop_data_file,"w");
foreach($buffer as $ke => $val){
#split items into fields
$itarray = explode('|', "$val");
if($itarray[0]==$id && $newid ==$id){
fwrite($data,"$entry");
$itemup= true;
}
if($itarray[0] < $newid && $itarray[0] != $id){
#write buffer
fwrite($data,"$val");
}
if ($itarray[0] >$newid && $itemup != true){
fwrite($data,"$entry");
$itemup = true;
}
if ($itarray[0] >$newid && $id != $itarray[0] && $itemup == true ){
fwrite($data,"$val");
$itemup = true;
#write buffer;
}
}
if ( $itemup != true){
fwrite($data,"$entry");
$itemup = true;
}
#close data file
fclose($data);
}
if($err==duplicateid){
echo "<p align=center><font color=RED size=4>Duplicate Item ID</font><br>Please use a different ID<br><a href=javascript:history.back();><font size=4>GO Back</a></font></p>\n";
break;
}else
echo '<h4 align=center>The file has been updated!</h4>';
break;
}