SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    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:

    $line=ereg_replace("<oldURL>","<newURL>",$line);

    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!

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Sausalito, Ca
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    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...


Bookmarks

Posting Permissions

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