php fopen() parameters

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

php fopen() parameters

Post by Reason7194 on Tue Mar 29, 2011 10:19 pm
([msg=55737]see php fopen() parameters[/msg])

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 study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: php fopen() parameters

Post by fashizzlepop on Tue Mar 29, 2011 11:51 pm
([msg=55742]see Re: php fopen() parameters[/msg])

How do you mean "in the middle?" Like, directly in the middle? Or at a certain place somewhere inside?

I'd suggest opening the whole file and then search for the line you want to add something in at. Use Regexes or something.

Just an idea because it wasn't all too clear what you are trying to accomplish.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: php fopen() parameters

Post by OnlyHuman on Wed Mar 30, 2011 3:04 am
([msg=55744]see Re: php fopen() parameters[/msg])

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?
OnlyHuman
Poster
Poster
 
Posts: 191
Joined: Sat Aug 22, 2009 1:37 am
Blog: View Blog (0)


Re: php fopen() parameters

Post by Reason7194 on Wed Mar 30, 2011 8:02 am
([msg=55748]see Re: php fopen() parameters[/msg])

Sorry for my unclear question. When I use fwrite(), the content that I am writing goes directly to the bottom of the page of the file that it is being written to. Outside of all tags.

Example:
Code: Select all
<html>   blah blah blah </html> Content of fwrite()


And since the content of fwrite() is outside of all tags, it does not go within the tags it needs to be between in order to appear a certain way.

@ 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().
I study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)


Re: php fopen() parameters

Post by OnlyHuman on Wed Mar 30, 2011 9:56 am
([msg=55749]see Re: php fopen() parameters[/msg])

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.

Code: Select all
<?php

   include("Links.php");

   function displayPosts()
   {
      // make sure file exists, create it otherwise
      if( !file_exists("support.php") )
      {
         $fp = fopen("support.php","w");
         fclose($fp);
      }

      // now open out file for reading
      $fp = fopen("support.php", "r");

      // ensure valid file
      if( $fp )
      {
         // display each post
         while( !feof($fp) )
         {
            $line = fgets($fp);

            $text = htmlentities($line);

            if( $text )
               echo $text . "<br />";
         }
      }

      // and close
      fclose($fp);
   }

   // 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'];

      $fp = fopen("support.php","a");

      // a better use of quotes here: Solves the problem of '$message'
      // being written to the file instead of the message content and
      // appends a new line to each entry.
      fwrite($fp, "\n$message");

      fclose($fp);
   }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
      <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
      <link rel="stylesheet" type="text/css" href="style.css" />
      <title>PUT YOUR PAGE TITLE HERE</title>
   </head>
   <body>
      <form method='post' action=''>
         Username:<input name='username' type='text' /><br />
         Message:<br />
         <textarea name='message' rows='15' cols='40'></textarea><br />
         <input type='submit' />
      </form>
      <div id="user-content" name="user-content">
<?php displayPosts(); ?>
      </div>
   </body>
</html>

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.
OnlyHuman
Poster
Poster
 
Posts: 191
Joined: Sat Aug 22, 2009 1:37 am
Blog: View Blog (0)


Re: php fopen() parameters

Post by Reason7194 on Tue Apr 05, 2011 10:08 pm
([msg=56012]see Re: php fopen() parameters[/msg])

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? )

Here is the syntax that you created with the fseek that I added:
Code: Select all
<?php

   include("Links.php");

   function displayPosts()
   {
      // make sure file exists, create it otherwise
      if( !file_exists("repost.php") )
      {
         $fp = fopen("repost.php","w");
         fclose($fp);
      }

      // now open out file for reading
      $fp = fopen("repost.php", "r");

      // ensure valid file
      if( $fp )
      {
         // display each post
         while( !feof($fp) )
         {
            $line = fgets($fp);

            $text = htmlentities($line);

            if( $text )
               echo "$text <br />";
         }
      }

      // and close
      fclose($fp);
   }

   // 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 />");

      fclose($fp);
   }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
      <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
     
<link rel="stylesheet" href="menu.css" type="text/css"/>

<link rel="stylesheet" href="style.css" type="text/css" />

<link rel="stylesheet" href="Sumry.css" type="text/css" />
      <title>Post Area</title>
   </head>
   <body>
      <form method='post' action='supported.php'>
<br>
<br>
<br>
         Username:<input name='username' type='text' /><br />
         Message:<br />
         <textarea name='message' rows='15' cols='40'></textarea><br />
         <input type='submit' />
      </form>
   
   </body>
</html>


Here is the actual fseek area:
Code: Select all
   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 />");

      fclose($fp);
   }
I study Gotafu.
Reason7194
Poster
Poster
 
Posts: 215
Joined: Fri Jan 07, 2011 5:01 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests