Solved

How can I use PHP to update record in flat file

Posted on 2008-06-16
9
738 Views
Last Modified: 2012-05-05
I have a flat file/PHP based poll system.  Using a database is out of the question on the server it is on.

So far, I have it working except for updating a row with the new results.  I can write to the file, I just don't know how to replace just that one row.

My text file is:
Poll ID||Question||Yes Votes||No Votes
107||Do you like this so far?||3||2
108||Do you like this so far?||3||2
109||Do you like this so far?||3||2
110||Do you like this so far?||3||2
111||Do you like this so far?||3||2
112||Do you like this so far?||3||2
113||Do you like this so far?||3||2
poll-vote.php.txt
0
Comment
Question by:baumli1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
9 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 84 total points
ID: 21797682
Here is one possible solution. In this case, you read the entire contents of the file into an array, search the array for a specific value of Poll ID. If the value matches, update the string.


<?php
 
// Change this filename as necessary
$filename = "flatfile.txt";
$fh = fopen($filename, 'r');
 
$contents = fread($fh, filesize($filename));
 
fclose($fh);
 
// If you are on a Windows server, change "\n" to "\r\n"
$arrLines = explode("\n", $contents);
 
foreach ($arrLines as $key=>$line)
{
	$arrTemp = explode('||', $line);
	if ($arrTemp[0] == '111')
	{
		$arrTemp[1] = 'This is the new value';
		$outputString = implode('||', $arrTemp);
		$arrLines[$key] = $outputString;
	}
}
 
// If you are on a Windows server, change "\n" to "\r\n"
$outputContents = implode("\n", $arrLines);
 
// You will probably want to write to the same file as before, rather than to a new file.
// Change this line as necessary
$fh = fopen('flatfile2.txt', 'w');
fwrite($fh, $outputContents);
fclose($fh);
 
?>

Open in new window

0
 
LVL 17

Expert Comment

by:jasonsbytes
ID: 21797688
what you need to do is write the entire file to a temporary file with your changes, then move the temp file back to the original file.

There is no way to modify just a line in a file using php.

Make sense?
0
 

Author Comment

by:baumli1
ID: 21797878
I've almost got it I think.  I store all the contents in an array anyways.  So after I'm done with all the processing, I can write the array back out to the flat file.  The only problem now is that it just appends the array to the end of the flat file.  Is there a way to empty out the file first, or just overwrite the contents with the new array?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:glcummins
ID: 21797899
To truncate the file, when you open the file the second time for writing, use:

 fopen($filename, 'w');

The 'w' says:

 "Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it." (http://www.php.net/manual/en/function.fopen.php)
0
 

Author Comment

by:baumli1
ID: 21797970
I actually never closed the file before writing to it again, so I used an "r+" when I first opened it.  And that did the trick.

The only think I worry about it keeping the results file open, while other people may be accessing it.
0
 
LVL 17

Assisted Solution

by:jasonsbytes
jasonsbytes earned 83 total points
ID: 21798018
after you open it and load it into an array, close it...

then write the new data to a temp file...
then move the temp file back to the original...

moving is faster than writing and should prevent someone from opening it while it's being written to.
0
 
LVL 1

Assisted Solution

by:kumarakam
kumarakam earned 83 total points
ID: 21798042
Here is a sample that can search and replace as many text in one step. It reads a from one file and writes to another file.


<?php
$infile = "$myfilein";
$outfile = "$myfileout";
 
$patterns[0] = "first old text to be replaced";
$replacements[0] = "first new text to replace";
 
$patterns[1] = "second old text to be replaced";
$replacements[1] = "second new text to replace";
 
$oldcontent = file_get_contents($infile);
$newcontent = preg_replace($patterns, $replacements, $oldcontent);
$data = file_put_contents($outfile,$newcontent);
?>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question