1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP - How to read file WITHOUT locking it

Discussion in 'PHP' started by geforce, Jul 17, 2010.

  1. #1
    Hi,

    Im hoping someone can help me with a small problem im having.

    I have a program which is logging actions from a windows application. This application puts actions into a text file which im reading and formatting to put into a database. All that is working fine.

    But! When reading the log file with PHP if the program wants to add an action to that log file it says it cant create the log file, obviously because im reading it with php at that time.

    Is there anyway I can read it but also allow the application to write to it no matter what. I can open the log file in notepad and the application will strill write to it.

    Thanks for any help.
    Martin
     
    geforce, Jul 17, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    use file_get_contents
     
    gapz101, Jul 17, 2010 IP
  3. geforce

    geforce Active Member

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #3
    Well my thread explains that I am already reading the file, I do know how to read files with PHP.

    My problem is reading it WITHOUT stopping the program from writign to it.
     
    geforce, Jul 17, 2010 IP
  4. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #4
    make a copy, then read it :)
     
    gapz101, Jul 17, 2010 IP
  5. caciocode

    caciocode Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Have you tried fopen () with 'w+' mode?
     
    caciocode, Jul 17, 2010 IP
  6. geforce

    geforce Active Member

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #6
    I've tried copying then reading, has the same problem.

    How would "w+" help? Im wanting to read it.
    I have no control over how the windows application writes to the file.

    At the moment im using:

    
    
    $log_location = "path to file";
    
    $handle = fopen( $log_location, "r" );
    $contents = fread( $handle, filesize( $filename ) );
    fclose( $handle );
    
    PHP:
    Cheers
     
    geforce, Jul 17, 2010 IP
  7. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    File locking in windows is pretty tedious, in *nix operating systems everything revolves around the system call flock.
    I know PHP has a function named the same - I have no idea how they have implemented it on windows (if they actually have).

    Maybe you could try something like this: (untested)
    
    <?php
        $log_location = 'path to file';      
    
        $handle = fopen($log_location, 'r');
        flock($handle, LOCK_SH) || die('Could not obtain file lock.');
        $contents = fread($handle, filesize($filename));
        fclose($handle);
    ?>
    
    PHP:
    If that doesn't work, there are command line programs out there that can release file locks. You could execute it from the script, then open it for reading.
     
    Deacalion, Jul 18, 2010 IP