Results 1 to 2 of 2
Thread: Writing Files Using PHP
Mar 27, 2001, 12:26 #1
I'm trying (really) to RTFM, but I can't seem to figure out the most basic of file operations in PHP. I have a series of about 200 HTML files, and, for reasons that I won't go into, they need to stay as HTML files. I need to change some URLs in every single one of them and I don't want to do it all manually.
The files are sequentially numbered, so I can easily loop through them all. I'm assuming that once a file is open I can loop through each line and use ereg_replace to change the URLs:
or something like that. So... what command do I use to open the file in the first place? "file"? "fopen"? And, how do I then save the file with the changes? I'm guessing that I probably have to open each file, read the contents into a variable/array, clear the file, and then use the variable/array to write everything back to the file, making the changes, of course. I just can't figure out how, exactly. Can anyone help? Thanks!
Mar 27, 2001, 12:43 #2
- Join Date
- Mar 2001
- Sausalito, Ca
- 0 Post(s)
- 0 Thread(s)
You do have to use fopen($file,w). The "w" switch allows you to open a file in write mode. $file represents the file name. I do something like this:
first, open a file. If you want to create a new file, use the exact same code as below. Php will look to see if the file exists, and if it does not, it will create a file with that name.
$fileToOpen = fopen ($file, "w");
you then need to pass the var for the opened file, the string or content to be written to the file, and a character limit
fwrite ($fileToOpen,$stringToWrite, 100000);
That's it, your done.
Hope this helps...