• 0

[PHP] File Based Shoutbox problem.


Question

Hi, I am currently working on a file based Shoutbox. Instead of using database, I decided to use a file to hold the data. You can preview the shoutbox at: http://animeasylum.3.forumer.com/index.php?act=idx

I am currently working on a feature to edit the posts. I am able to get the data from the file, and display the data. Once the user has edited the post and submitted it. If the length of the updated shout is longer than the strength of the previous shout, the fwrite function goes on to the next line in the file and rewrites other shouts.

My Question. How can I add the updated version of the shout without replacing previous data?

My Solution: I can make do it so that once I have reached the point of the shout data that needs to replaced, I extract the remaining data and store it in the memory space. Then add the newest data and then fwrite the remaining data into the file. I think that may be a little hassle.

So I was wondering if it is possible to fwrite the data, and only limit the boundary to the current line the indicator is on?

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

THat works if the original post is short than the edited version. Here is what I did.

Found the line in the text document with the id of the post that is to be edited. Replaced the text of the old with the new one.

Then I Replaced the line of cached text of the file with the new line. Then I fwrite the updated to the file. The problem is, if works fine if the updated version of the post is bigger. It is able to replace the whole document with the new data. But, if the updated post text is smaller in comparison to the original, then the file would be written, and some of the of the text in the previous file is not replaced. Now, I dont know how I can delete the extra stuff. I can use fseek to set the pointer to where the newest file will end, but I dont know how I can delete the part of the file from the end of the newest file, to the filesize of the original file.

Any idea, or different way of doing this?

Link to comment
Share on other sites

  • 0

Ah, sorry, thought you had one way of doing it if the original was < the edited, and a way of doing if the original > the edited. MB.

How about concatting the whole file into one string, exploding it by newlines and then overwriting the relevant line?

Something like

$filestring = "";
// Loop through every line in your file and apped it to $filestring
// $filestring .= whatever;

$newfile = explode ("\n", $filestring);

// Find the line that needs changing and take 1 away from it

$newfile[$linenum] = "New string";

$newfile = implode("\n", $newfile);

Yea/nay?

Link to comment
Share on other sites

  • 0

As I said in my second post, that is what I do.

here is the code I have.

				$shout = $_POST['comment'];
				$shoutid = $_POST['commentid'];
			  $IP = $_SERVER ["REMOTE_ADDR"];
				$handle = fopen($filename, "r+");
   			$shoutsString= fread($handle, filesize($filename));
   			$shoutsArray = split("\n", $shoutsString);
	 			for ($x=0; $x &lt; count($shoutsArray); $x++)
		   {
			  $lastShout= $shoutsArray[$x];
		$shoutArray = split(",", $lastShout);
							if ($shoutArray[0] == $shoutid)
								 {
								 $x =count($shoutsArray);
								 }
		   }

				$data= str_replace($shoutArray[4],$shout,$lastShout);
				$newData= str_replace($lastShout,$data,$shoutsString);
				rewind($handle);
				fwrite($handle, $newData);	 

The problem. Lets say the current filesize is 467bytes, but the next text only takes up 400bytes. I can write the 400 bytes of the file, but the data after 400 bytes are still in the file. How can I remove those extra bytes? I am thinking of deleting the file, and creating a new file with the new data. Just little worried if the system is not fast enough and some other user has made a post at the same time, it might not be added.

What's your thoughts?

Link to comment
Share on other sites

  • 0

I decided to use ftruncate to solve the problem. The thing is, I am not fond of using this way. So if any one has a better idea or a different solution to solving the problem, please share.

Link to comment
Share on other sites

  • 0

IMO, this is a prime example of when not to use files. The data being thrown about is highly volatile and you are definately going to run into problems with file-locks blocking users as they attempt to concurrently access the file. With a larger number of users it could result in a queue of users trying to read and write the file (while its open for writing it's locked for reads too). It wouldn't surprise me to see php crash as more and more requests are added to the queue (each user will have multiple requests building up).

Database ftw.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.