Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Opening & inserting text into a file

    I'm just learning PHP & I'm just making up some simple scripts & such but now I am wondering how do you open a file & then add text to it?

    Also, is there a way to reference line numbers in files in PHP?

    Thanks much!

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Welcome here!

    Yes there is. You can open a file with different modes using fopen(), such as "a" for append, etc..

    If you look at that link you will find some example code as well as some user comments with different examples. You should be able to find what you're looking for there.

    Also, is there a way to reference line numbers in files in PHP?
    Not that I'm aware of. If you want line 52, you'll have to start at the top and loop through the file 52 times, one line at a time. That's as far as I know.. maybe some else can be of more assistance there.

    Hope that helps,
    Sadiq.

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you know how many bytes there are until that line starts, then you would be able to, but other than that you would have to loop through the file.

    And about writing to files: http://www.codingforums.com/showthre...320#post188320

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    OK thanks guys! I will have a look into those links & such!

    I couldn't do it by bytes as each insert would vary.

    Other than looping, is it possible to insert like a comment tag where u want the text (code) to go in the file & then it can just find the first instance of that line & insert it there?

    Cheers!

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I haven't had much experience with files, but I'd imagine that you could load the entire file into a variable and then mess around with it -- using regex or something -- and then overwrite the file with your new stuff..

    Don't know how off hand, you'd have to do some research on that.. php manual is a good place to start. The user comments are usually quite helpful as well.

    Good luck,
    Sadiq.

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what you are proposing is possible, however, if you have database access I would consider using the database over the textfile method, as it will be faster and less likley to go wrong, as well as being easier to manage.

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yeah... using a database probably would be a better way to go with this but I'm just doing it this way to get some experience with PHP, won't be using the script for anything, just seeing if I can code it successfully.. plus I haven't learned about implementing the code with databases yet heh..

    I haven't had much experience with files, but I'd imagine that you could load the entire file into a variable and then mess around with it -- using regex or something -- and then overwrite the file with your new stuff..
    I'm not sure how different PHP is, but with my experience in Perl you would need to load files into arrays... or can you do either with PHP?

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Loading files into arrays is indeed possible, the most basic way being:

    PHP Code:
    $array file("filename.txt"); 
    That will return the file contents in an array, split by newlines. As you are using this for testing it should be ok, but for extremely large files is not always good (as it could take forever).

    (As you are doing this for a learning experience, I have a couple more helpful hints)

    Another quick snippet that I use often, and that you may or may not be aware of is:

    PHP Code:
    <?php
    echo '<pre>';
    var_dump($var1$var2);
    echo 
    '</pre>';
    ?>
    Which will display a "dump" of the information in a variable (whether it an array, object or just a string or number. It is very useful for debugging scripts as well as learning more about what the code is generating...

    Finally, when you are learning (or indeed debugging), you can use:

    PHP Code:
    error_reporting(E_ALL); 
    at the top of your page to make sure all errors are shown


    As for database functions, Im assuming that you would be using MySQL, so you could look at http://www.php.net/mysql

    Woah, drifted a bit here eh

  • #9
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks... I will check into all that & try & remember & learn it all lol

    Quote Originally Posted by missing-score
    Loading files into arrays is indeed possible, the most basic way being:

    PHP Code:
    $array file("filename.txt"); 
    That will return the file contents in an array, split by newlines. As you are using this for testing it should be ok, but for extremely large files is not always good (as it could take forever).
    Hmm again, I'm just learning PHP so not sure exactly PHP works, but, isn't that just an variable called "array"?

    Don't you do arrays like this:

    PHP Code:
    $file = array ( "filename.txt" ); 
    or would that be:

    PHP Code:
    $file = array file"filename.txt" ); 
    ?

    Thanks!
    Last edited by cyphix; 05-01-2004 at 10:54 AM.

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the first code you posted will indeed make an array, but it will only hold the data of the filename you put in.

    The file function will return an array of data in the file... http://www.php.net/file


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •