SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Open, edit and save a file all at once

    I use the following code in PERL to open, edit and save a file all at once.

    Code:
    open (FILE, "+<counter.txt");
    flock(FILE, 2);
    $number = <FILE>;
    $number = $number + 1;
    seek (FILE, 0, 0);
    truncate (FILE, 0);
    print FILE $number;
    close(FILE);
    I want to do the same thing in PHP. How do I do this? I don't want to open, read, close then open, write, close. I want to do all at once like the code above.

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I don't know Perl but it appears that code is opening, reading, truncating, writing then closing right?
    PHP Code:
    $filename "whatever.txt";
    $file fopen($filename"r+");
    $number fread ($filefilesize ($filename));
    $number++;
    rewind($file);
    fwrite($file$number);
    fclose($file); 
    Untested code. I've never read in just a single number from a file. But it should work.
    http://www.php.net/manual/en/ref.filesystem.php


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
  •