Hello everyone, I have a small question on php fopen() parameters. The problem I am having with the 'w' and other letter type parameters is that the parameters that allow for a addition of content to the chosen file, they add to the end of the file. Never the middle. Now when I am adding content to a file with fwrite and fopen, they are added outside the style marks I have so that the content would appear a certain way. How would I set up fopen or something else so that the content would be added in the middle of the selected file, not on the end.

I would just place all of the style information in a separate CSS file and then link to it from the file needing that style information. That would eliminate the need to worry about where the data is stored when using a single file.

Question: Is this based on what you're working on with your other thread?

Reason7194 wrote:@ OnlyHuman - Partly, I'm never sure to keep all the same topic question, but slightly different, in the same thread or not. If so, I'll delete this and put it in the previous thread on fwrite().

No, it's cool. I just wanted to make sure we're on the same page here, in order to better formulate an answer that might help. Plus I don't think can delete in this forum. I know you can in the NZone, but I'm not sure about here.

Anyway, what you're working on, is basically a CMS (Content Management System). Except in this case, you're using a php file to store some user provided content, as opposed to a database. I don't necessarily believe that the stumbling blocks you're currently experiencing are the result of not understanding the file operations. Instead, I think that the bulk of it lies in a fully developed understanding of how each piece functions independently, in order to create the whole.

And I say that because, what you're wanting to do with the file operations, involves opening the file in read+write ("r+") mode, seeking to the last HTML tag, reading everything past that tag, and THEN placing it in the proper spot each time the form is submitted. That can be done, but it's a lot of excess trouble, and with a different design, you can avoid all that.

I'm just going to use your code from before, rewritten a bit, and work from there. I've gone ahead and thrown a copy of this on my local system, so I know that it works as written, and you shouldn't have any problem executing it either.

Take a minute to really examine that changes that were made. Notice that the procedure that was used to display message content has been moved to a separate function now. And aside from a single call to that function, all the PHP code is self contained, followed by HTML written directly into the file. This is valid, though not the only way to do it. I took out your center tags, because it's assumed that positioning and style information, is now moved to the CSS file, pointed to in the header by the link tag. Which is also why I gave that div following the form the 'id' and 'name', so that they're easier to manipulate from the CSS file.

The difference between this, and what might have been causing problems before, is that everything is now separated into individual components, which function as a whole. You can use any additional HTML before and after the call to displayPosts(), and the content from 'support.php' will take advantage of that HTML, without having to worry about where things are located within the file. That's simply because, the only data that gets written to 'support.php' is the actual message itself. Everything else is handled externally. Which is where I believe the bulk of the confusion originated, and not in the use of the file functions. Normally you would actually store these in a database, but since your working on understanding those file functions more thoroughly, this works too.

I hope all this makes sense, and actually helps solve the problem. If not, rather than focus on file operations, it might be best to study CMS design for a bit. Just so you get a better understanding of what I'm trying to say here. Then you'll see that the problems aren't with how you're using the file functions, but in the way those files are working together.

Hey OnlyHuman, so since you have posted I have been learning learning and learning about what you wrote. Many of the terms you used such as the eof term I had never heard about haha. What you wrote really helped me understand what I had in my head, the CMS system. However, i now have a related question to what I have added your script. Instead of posting the variable 'message' to the eof, I placed the content of 'message' at the beginning of the file with fseek. My problem though, is that I cannot get a break to occur between posts. I could make a break happen with the eof, so is there something different that you have to do with fseek to use html for things like breaks and new lines? ( Are they the same thing? )

// FIXED: Case sensitivity mismatch in original $_POST['message']. if( isset($_POST['message']) && ($_POST['message'] != null) ) { // this should actually be filtered as well $message = $_POST['message']; //Opens the file $fp = fopen("repost.php","r+"); //Moves the cursor back to beginning of the file. The '0' represents the character location. fseek($fp,0); //Writes the content of variable "message" to the pre-specified file. fwrite($fp, "<br>$message<br />");

if( isset($_POST['message']) && ($_POST['message'] != null) ) { // this should actually be filtered as well $message = $_POST['message']; //Opens the file $fp = fopen("repost.php","r+"); //Moves the cursor back to beginning of the file. The '0' represents the character location. fseek($fp,0); //Writes the content of variable "message" to the pre-specified file. fwrite($fp, "<br>$message<br />");