Home >Backend Development >PHP Tutorial >PHP file locking ensures multi-threaded writing safety

PHP file locking ensures multi-threaded writing safety

不言
不言Original
2018-04-24 11:05:592151browse

The content of this article is about PHP file locking to ensure the safety of multi-threaded writing. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

[php] view plain copy

    function write_file($filename, $content)  
    {  
        $lock = $filename . '.lck';  
        $write_length = 0;  
        while(true) {  
            if( file_exists($lock) ) {  
                usleep(100);  
            } else {  
                touch($lock);  
                $write_length = file_put_contents($filename, $content, FILE_APPEND);  
                break;  
            }  
        }  
        if( file_exists($lock) ) {  
            unlink($lock);  
        }  
        return $write_length;  
    }

Related recommendations:

Error type of php file upload error


The above is the detailed content of PHP file locking ensures multi-threaded writing safety. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php5.6.34 install XdebugNext article:php5.6.34 install Xdebug