Search code examples
phpcopyfile-exists

Multiple requests conflict time to read or copy a file


Details:

  • PHP Version: 5.3.0
  • Apache 2.2.37
  • Windows XP SP3

I am creating a system backup error (php error and custom error) in TXT, not user data but the dynamically generated page. If you have already generated the LOG other user does not need to generate as ever existed.

I simulated 10 concurrent connections/requests and sometimes this error occurs: No such file

Code:

<?php
$p = 'errs/'.$arqErr;
$o = 'temp/'.$arqErr;
if(file_exists($o) && is_readable($o) && is_writable($o)){
    if(!copy($o,$p)){
        echo 'Error copy "',$p,'" to "',$o,'"';
    }
    if(file_exists($o) && is_readable($o) && is_writable($o)){
        unlink($o);
    }
}
?>

sometimes the error occurs: Warning: Unable to open 'temp/6039dd66559c9431004109202d279557.php' for reading: No such file or directory

and sometimes the error occurs: Warning: Unlink failed (No such file or directory)

Is it a bug of PHP_5.3.0?

Note: In Windows Seven with PHP5.3.4 this failure does not occur.


Solution

  • The problem seems to be having multiple concurrent users trying to access the same files, it doesn't seem to be a PHP related problem.

    Try to think of a different logging scheme, maybe using the error_log() function you can have PHP manage concurrency access issues for you.

    <?php
    error_log('Log message', 3, "md5(address of the page).LOG.txt");
    ?>