I have a project that, instead of using MySQL, I am supposed to use txt files to manage data. I have no say in this, I'm doing the project for a client.

So I wrote a test file with functions for writing these data files, reading, etc. But it's not acting correctly. I call read the content of a file, let's say it has 1 line in it. I then call the function to write an entry to the file. I then read the contents of the same file again, but it still shows only the 1 entry.

I then comment out the call to append data to the file, and refresh the page, which now shows that there are 2 lines in the file instead of the one.

Does it make sense? Here is my code. It's pretty straight forward.

I was hoping you could point out why it's not reading the file accurately, and maybe, if it's not obvious, give me an idea to fix it. Thanks!

PS If you have any use for this code, take it, it's yours! Just send me a copy if you get it working right :)

<?php

// To make sure the file exists, etc
function prep_log($file)
{
if(!file_exists($file) || filesize($file) < 1){
$fh = fopen($file, 'a');
fwrite($fh, "phone,carrier_name,carrier_domain,captcha,ip,agent,script,time\n");
fclose($fh);
}
}

// Append a single entry to the file
function append_log($file,$content)
{
prep_log($file);
$fh = fopen($file, 'a') or die("can't open file");
fwrite($fh, $content);
fclose($fh);
}

// Completely over write a file with new content
function overwrite_log($file,$content)
{
prep_log($file);
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, $content);
fclose($fh);
}

// Read the log file contents
function read_log($file)
{
prep_log($file);
$fh = fopen($file, 'r') or die("can't open file");
$content = fread($fh, filesize($file));
fclose($fh);
return($content);
}

/*
Since the log files keep track of data as csv, we can remove a line based off 
of what a particular column contains. So this function removes a line from the log file where a there is a match.
*/
function remove_from_log($file,$position,$match)
{
prep_log($file);
$content = read_log($file);
$line = explode("\n",$content);
foreach ($line as $k => $v)
{
$v = trim($v);
if(!empty($v)){  //$out = trim($out);
  $pcs = explode(",",$v);
  if(str_replace("\"","",$pcs[$position-1]) != $match){$out .= "$v\n";}
}
}
overwrite_log($file,$out);
}

function write_to_log($file,$array)
{
// Compile $array into a csv line
$ins = "\"".$array['phone']."\",\"".$array['carrier']['name']."\",\"".$array['carrier']['domain']."\",\"".$array['captcha']."\",\"".$array['ip']."\",\"".$array['agent']."\",\"".$array['script']."\",\"".$array['time']."\"\n";
append_log($file,$ins);
}

/******************************************************************************/
// Here is the data I will add to a log file:
$array['phone'] = "1231231234";
$array['ip'] = "123.123.123.123";
$array['agent'] = "User Agent Test";

// What file to write to:
$file = "test.txt";

// Read the log file to the screen
echo "<pre>".read_log($file)."</pre>";

// Add an entry to the log file
write_to_log($file,$array);

// Read the log file to the screen. This SHOULD show me the log file + the line that was just added, but instead it shows the same exact data as the read_log() we execute a few lines up.
echo "<pre>".read_log($file)."</pre>";

?>

Any ideas? Thank you!

Here is a script that illustrates the same problem. Thanks

<?php

$file = "test.txt";
$array['phone'] = "1231231234";
$array['ip'] = "123.123.123.123";
$array['agent'] = "User Agent Test";
$ins = "\"".$array['phone']."\",\"".$array['carrier']['name']."\",\"".$array['carrier']['domain']."\",\"".$array['captcha']."\",\"".$array['ip']."\",\"".$array['agent']."\",\"".$array['script']."\",\"".$array['time']."\"\n";

//prep file
if(!file_exists($file) || filesize($file) < 1){
$fh = fopen($file, 'w');
fwrite($fh, "phone,carrier_name,carrier_domain,captcha,ip,agent,script,time\n");
fclose($fh);
}

//print file to screen
echo "<pre>";
$fh = fopen($file, 'r') or die("can't open file");
echo fread($fh, filesize($file));
fclose($fh);
echo "</pre>";

//append to file
$fh = fopen($file, 'a') or die("can't open file");
fwrite($fh, $ins);
fclose($fh);

//print file to screen
echo "<pre>";
$fh = fopen($file, 'r') or die("can't open file");
echo fread($fh, filesize($file));
fclose($fh);
echo "</pre>";


?>

I've figured out something to help me make the script work properly:

<?php
$file = "text.txt";
echo filesize($file);
clearstatcache();
echo filesize($file);
?>

This also clears the cache the fopen, function(s) use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.