Forum Moderators: coopster

Message Too Old, No Replies

Read from one file and Write to another

Is this possible?

         

coho75

5:42 pm on Jul 10, 2004 (gmt 0)

10+ Year Member



What I need to do is read from file A and then take what is read and place it in a certain spot in file B. Is there a way to do this with PHP? I know that a file can be read and then altered, but I have been unable to find a lot of information about reading from one file and writing to another.

coho75

jatar_k

6:25 pm on Jul 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



dead easy, there are a bunch of threads around with specific code examples but the logic would be as follows.

1. fopen (for reading) your source file
2. fopen (for reading) your target file with a different file pointer
3. fgets or fread to get the data from source
4. read the target file into a var until you find the spot
5. put the data from the source into the var instead of the target file data
6. pull the rest of the data from the target file into the var
7. fopen the target file for writing, which will make it zero length
8. write the new data from the var to the target file
9. fclose all file pointers

coho75

7:57 pm on Jul 10, 2004 (gmt 0)

10+ Year Member



Thanks a lot Jatar_K. The information you provided was exactly what I was looking for. Got the program to work on the first try.

Thanks,
coho75

jatar_k

11:16 pm on Jul 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>Got the program to work on the first try

nice, very satisfying for both of us then :)