SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2003
    Location
    location!
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fputs - write to specific file location?

    hi,

    Is there a built in way to write with PHP to a specific external file location?

    i see with fputs and fwrite you can specify one of the following a, a+, r+, w and w+ that will either write to the end of the file or over write all existing contents.

    thanks for your help

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you begin with fopen() and it either opens a file or creates new one if the file doesn't exist, then you keep the handle to the file and use fwrite() ...

    example:
    PHP Code:
    if (!$handle fopen($filepath $filename'a'))
        die(
    'failed to open file...');
    $line 'some text to go in file';
    if (!
    fwrite($handle$line))
        die(
    'failed writing to file...');
    fclose($handle); 
    - website

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,608
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shaft7
    hi,

    Is there a built in way to write with PHP to a specific external file location?
    Do you mean to a specific point within the file? If yes then how are you going to determine where that specific point is? What are your criteria?
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Member
    Join Date
    May 2003
    Location
    location!
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    you begin with fopen() and it either opens a file or creates new one if the file doesn't exist, then you keep the handle to the file and use fwrite() ...
    Thats all fine except if you've got...

    <file.txt>
    value1=34
    valueWhatever=2
    value99=cheese

    and only want to update value99 by using fopen($filepath . $filename, 'a') we'll be left with...

    <file.txt>
    value1=34
    valueWhatever=2
    value99=cheese
    value99=bacon

    I was thinking of a way around this by writing all the files contents to an array using $someArray = file("pathtofile.txt");

    then searching out the exact line i need to update, then rewriting all the array contents back into the file by overwriting all the contents.

    Thoughts?

    thanks for your help
    Last edited by shaft7; May 14, 2003 at 05:31.

  5. #5
    SitePoint Member
    Join Date
    May 2003
    Location
    location!
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru
    Do you mean to a specific point within the file? If yes then how are you going to determine where that specific point is? What are your criteria?
    yes, i've setup a series of loops to find the value(s) i need to update however was hoping for a strightforward way to access that specific point in the file for the update.


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
  •