Read and Write file contents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    Read and Write file contents

    Hii this is pradeep here . i am a newbie to PHP .can u help me out.
    i hve a file called con.conf(config uration file) tht contains
    name:pradeep
    place:bangalore

    i need to read this file and display the contents in form type in browser . and then gve a submit button so as to edit and update the file .
    can u gve me a code for this process.
    email:[ REMOVED ]
    Last edited by ak1dnar; Jul 31 '07, 02:59 PM. Reason: Email Removed - thread title Changed from : Help This Newbie
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Checkout file() function of PHP.
    Do not post your email address and choose a descriptive title.
    Posting Guidelines

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      Originally posted by mwasif
      Checkout file() function of PHP.
      Do not post your email address and choose a descriptive title.
      Posting Guidelines

      but plzz gve me code for tht program if u can.i read tht file() but cld not do it. plzz can u help me wit code..i wll be great ful to u

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Originally posted by pradeepjain
        but plzz gve me code for tht program if u can.i read tht file() but cld not do it. plzz can u help me wit code..i wll be great ful to u
        Unless you have money, your "great"-fulness isn't worth much (at least not to me :-D).

        What you want to do is something that a database could do easily. If you really want to do it with a file, then go back to the documentation for the file() function and read the documentation for ALL of the file functions (fread(), fwrite(), fopen(), etc.) and make the code.

        It's not hard to decipher. I only help those who help themselves. Show some effort, and you'll get some assistance.

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          Originally posted by volectricity
          Unless you have money, your "great"-fulness isn't worth much (at least not to me :-D).

          What you want to do is something that a database could do easily. If you really want to do it with a file, then go back to the documentation for the file() function and read the documentation for ALL of the file functions (fread(), fwrite(), fopen(), etc.) and make the code.

          It's not hard to decipher. I only help those who help themselves. Show some effort, and you'll get some assistance.



          OK COOL..I KNOW ALL THE FILE OPERATIONS..BUT THE FILE CONTAINS ENTIES LKE
          NAME:PRADEEP
          PLACE:BLORE

          THE NAME AND PLACE SHLD BE A FIXED FIELD IN THE FORM AND THE REST MUST VARY..PLZZ HELP ME HOW TO FETCH THE VARIABLES FROM FILE....OR ELSE GVE ME SOME SUGGESTIONS AS TO HOW TO GO ON WIT IT.

          Comment

          • nathj
            Recognized Expert Contributor
            • May 2007
            • 937

            #6
            Originally posted by pradeepjain
            OK COOL..I KNOW ALL THE FILE OPERATIONS..BUT THE FILE CONTAINS ENTIES LKE
            NAME:PRADEEP
            PLACE:BLORE

            THE NAME AND PLACE SHLD BE A FIXED FIELD IN THE FORM AND THE REST MUST VARY..PLZZ HELP ME HOW TO FETCH THE VARIABLES FROM FILE....OR ELSE GVE ME SOME SUGGESTIONS AS TO HOW TO GO ON WIT IT.
            1) What have you got written so far? Posting code you are stuck with will get you better help.
            2) Have you read about the explode() function

            The explode() function will enable you to retreive the contents of your file into an array.

            I must say that the best way to do this would be with a database. It's not difficult, read some tutroials on W3Schools and you'll be up and running in now time.

            Cheers
            nathj

            Comment

            • pradeepjain
              Contributor
              • Jul 2007
              • 563

              #7
              Originally posted by nathj
              1) What have you got written so far? Posting code you are stuck with will get you better help.
              2) Have you read about the explode() function

              The explode() function will enable you to retreive the contents of your file into an array.

              I must say that the best way to do this would be with a database. It's not difficult, read some tutroials on W3Schools and you'll be up and running in now time.

              Cheers
              nathj


              FOR THIS I AM NOT SUPOSED TO USE A DB THTS THE PROB..CAN U GVE ME CODE FOR THIS ONE ..IF U CAN

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #8
                Originally posted by pradeepjain
                FOR THIS I AM NOT SUPOSED TO USE A DB THTS THE PROB..CAN U GVE ME CODE FOR THIS ONE ..IF U CAN
                Okay, so you can't use a database, that shouldn't be a problem.

                Have you tried using the explode() function I pointed out?

                Try that and let me know how you get on? When you are playing around with it you may also find print_r(arrayna me) useful.

                The explode function takes a string and converts it to an array, you can then print the array (print_r) to see what you've got and how you can use it best.

                Have a go and post the code you get stuck on. Having a go is the best way to learn.

                Comment

                • pradeepjain
                  Contributor
                  • Jul 2007
                  • 563

                  #9
                  Originally posted by nathj
                  Okay, so you can't use a database, that shouldn't be a problem.

                  Have you tried using the explode() function I pointed out?

                  Try that and let me know how you get on? When you are playing around with it you may also find print_r(arrayna me) useful.

                  The explode function takes a string and converts it to an array, you can then print the array (print_r) to see what you've got and how you can use it best.

                  Have a go and post the code you get stuck on. Having a go is the best way to learn.

                  see i wll tell how i wnt to move abt


                  1st open the file in read mode
                  then take each line in a string using a for loop or so
                  then using explode searching for the : i wll split name:pradeep into 2 and then assign it some 2 variables;
                  and then print them in the form .
                  then use $_post method to read from the form and then write them to the file..

                  do u hve any suggstions on this

                  Comment

                  • pradeepjain
                    Contributor
                    • Jul 2007
                    • 563

                    #10
                    Originally posted by nathj
                    Okay, so you can't use a database, that shouldn't be a problem.

                    Have you tried using the explode() function I pointed out?

                    Try that and let me know how you get on? When you are playing around with it you may also find print_r(arrayna me) useful.

                    The explode function takes a string and converts it to an array, you can then print the array (print_r) to see what you've got and how you can use it best.

                    Have a go and post the code you get stuck on. Having a go is the best way to learn.


                    after reading from the form say $_post['name']
                    how to write back to file in same format as
                    name:pradeep

                    Comment

                    • nathj
                      Recognized Expert Contributor
                      • May 2007
                      • 937

                      #11
                      Originally posted by pradeepjain
                      after reading from the form say $_post['name']
                      how to write back to file in same format as
                      name:pradeep
                      You need to try this for yourself, using the file functions referenced earlier. Have a go and post some code and the results if you run into trouble.

                      We are all prepared to help but we won't write the code for you.

                      Cheers
                      nathj

                      Comment

                      • pradeepjain
                        Contributor
                        • Jul 2007
                        • 563

                        #12
                        Originally posted by nathj
                        You need to try this for yourself, using the file functions referenced earlier. Have a go and post some code and the results if you run into trouble.

                        We are all prepared to help but we won't write the code for you.

                        Cheers
                        nathj

                        1st open the file in read mode
                        then take each line in a string using a for loop or so
                        then using explode searching for the : i wll split name:pradeep into 2 and then assign it some 2 variables;
                        and then print them in the form .
                        then use $_post method to read from the form and then write them to the file..

                        do u hve any suggstions on this
                        ok i am ready to try .but is the way i am going abt is it alright.

                        Comment

                        • pradeepjain
                          Contributor
                          • Jul 2007
                          • 563

                          #13
                          Srry If I Am Bothering U All

                          Comment

                          • nathj
                            Recognized Expert Contributor
                            • May 2007
                            • 937

                            #14
                            Originally posted by pradeepjain
                            Srry If I Am Bothering U All
                            You are not bothering us all, this is what forums are for. It's just that the best way to learn is to have a go.

                            Originally posted by pradeepjain
                            1st open the file in read mode
                            then take each line in a string using a for loop or so
                            then using explode searching for the : i wll split name:pradeep into 2 and then assign it some 2 variables;
                            and then print them in the form .
                            then use $_post method to read from the form and then write them to the file..

                            do u hve any suggstions on this
                            ok i am ready to try .but is the way i am going abt is it alright.
                            This process sounds okay to me, I think you are heading down the right road with this idea. Also it's good to plan the process out before you write so full marks there.

                            I look forward to seeing the result. I'm sure you'll get this working as you need it to and that you will understand what you have done and why it works.

                            Cheers
                            nathj

                            Comment

                            • pradeepjain
                              Contributor
                              • Jul 2007
                              • 563

                              #15
                              <?php
                              $file=fopen("/var/lib/penguin/Desktop/welcome.txt","r ")or exit("Unable to open file!");
                              $x=0;
                              for($i=0 ; $i != feof($file) ; $i++)
                              {
                              $data = fgets($file,$x) ;
                              $str = explode(':', $data);
                              $name[$i] = $str[0]; // piece1
                              $value[$i] = $str[1]; // piece2
                              echo $name[$i];
                              echo $value[$i];
                              $x=$x+1024;
                              $i=$i+1;
                              }
                              $lcount=$i;
                              ?>
                              <html>
                              <head>
                              </head>
                              <body>
                              <?php
                              print $lcount;
                              ?>
                              <FORM METHOD=post ACTION="test1.p hp">
                              for($i=0;$i <= $lcount ;$i++){
                              <input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

                              }
                              <INPUT TYPE=SUBMIT>
                              </form>
                              </body>
                              </html>


                              i hve written this code...there is some error in the html combining wit the php..
                              can any one sujjest me something....

                              Comment

                              Working...