...

View Full Version : Opening & inserting text into a file



cyphix
04-30-2004, 08:27 PM
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!

sad69
04-30-2004, 08:48 PM
Welcome here!

Yes there is. You can open a file with different modes using fopen() (http://ca.php.net/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.

missing-score
04-30-2004, 09:30 PM
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/showthread.php?p=188320#post188320

cyphix
04-30-2004, 10:21 PM
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!

sad69
04-30-2004, 10:31 PM
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.

missing-score
04-30-2004, 10:34 PM
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.

cyphix
04-30-2004, 10:44 PM
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?

missing-score
04-30-2004, 11:44 PM
Loading files into arrays is indeed possible, the most basic way being:



$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
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:



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 :rolleyes:

cyphix
05-01-2004, 10:52 AM
Thanks... I will check into all that & try & remember & learn it all lol


Loading files into arrays is indeed possible, the most basic way being:



$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:



$file = array ( "filename.txt" );


or would that be:



$file = array file( "filename.txt" );


?

Thanks!

missing-score
05-01-2004, 11:17 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum