PHP file locking ensures multi-threaded writing safety

不言
Release: 2023-03-24 18:52:02
Original
2084 people have browsed it

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;  
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!