Read and write flat files using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • riyadh81
    New Member
    • Jul 2007
    • 3

    Read and write flat files using php

    hello

    i want to store submitted data of forms to a text file and retrieve from there, coz my hosting server package does not have the features of mysql database. can any one help me in this issue..

    best regards
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Originally posted by riyadh81
    hello

    i want to store submitted data of forms to a text file and retrieve from there, coz my hosting server package does not have the features of mysql database. can any one help me in this issue..

    best regards
    Hi there,
    This a simple script to write such data to text file.Commonly saying flat files.

    Write user inputs :

    [code=php]
    <?php
    if (isset($_POST['submit'])) {
    $Fname = $_POST['fname'];
    $Lname = $_POST['lname'];
    $fp = fopen("data.txt ","a");// Open data.txt for writing
    if(!$fp) {
    echo 'Error, the file could not be opened or there was an error creating it.';
    exit;
    }
    if(@fwrite($fp, $Fname.'|'.$Lna me."\n")){
    echo 'Record Added';
    }else{
    echo 'Failed';
    }
    fclose($fp);
    }
    ?>
    <html>
    <body>
    <form name="input" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
    <p><label for="firstname" >First Name: </label>
    <input type="text" name="fname" id="fname" /></p>
    <p><label for="firstname" >Last Name: </label>
    <input type="text" name="lname" id="lname" />
    <p><input type="reset" name="reset" value="reset" />
    <input type="submit" name="submit" value="submit" /></p>
    </form>
    </body>
    </html>

    [/code]

    Read flat file back.

    read this thread

    But I will never recomend this type of flat file system for record handling. its better only for few records only. when the file gets lagrer it won't be able to mange anymore.

    Thanks!
    -Ajaxrand

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Thread title changed to better describe the scenario

      Earlier: want to store submitted info to text file
      Recent: Read and write flat files using php

      Comment

      • riyadh81
        New Member
        • Jul 2007
        • 3

        #4
        thank you very much for your perfect guidance.

        the script you provided was perfect n worked. if i want to show the insert data in of text file to php page. how can i do that thing

        thanking you

        Comment

        • ak1dnar
          Recognized Expert Top Contributor
          • Jan 2007
          • 1584

          #5
          Originally posted by riyadh81
          thank you very much for your perfect guidance.

          the script you provided was perfect n worked. if i want to show the insert data in of text file to php page. how can i do that thing

          thanking you
          Do you want to read the inserted records from flat file again.
          Read the Thread Link I gave in my previous post.

          Comment

          Working...