Using php fseek

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

Using php fseek

Post by Reason7194 on Tue Apr 12, 2011 11:36 am
([msg=56275]see Using php fseek[/msg])

Hello everyone,I had placed this same question in another thread but sadly it was never answered.

This is a response to one of 'OnlyHumans' reply in the thread this question was posted in: 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+");
       
          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)


Re: Using php fseek

Post by thetan on Tue Apr 12, 2011 9:22 pm
([msg=56284]see Re: Using php fseek[/msg])

You need to stop worrying about fseek and seriously need to reconsider your approach. Naively raw dogging unprocessed text like that is something that would have been frowned upon even in the 80s.

the obvious bug is the following line:
Code: Select all
         //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 />");


The problem is you fseek to the beginning of the file and write to it. Well, what happens is you end up overwriting whatever was at the beginning of the file before. IE: this is like having a file with the contents AAAAAAAAAA and then opening it, fseek'ing to 0 and writing BBBBBB, the file will then contain BBBBBBAAAA.

As i said, this is more of an implementation flaw then anything though.

You should never make such assumptions on unprocessed text unless it happens to be highly domain specific and _alot_ is known about it, and thats usually fairly advanced.

So what you need to do is read and parse the file into a domain specific object that you can safely interact with. Once the file is parsed into a data structure you can actually work with you can go about walking it and displaying it however you want. Persisting the data it to disk should be another straightforward level of abstraction.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests