private function tryRemoveWithLock(string $fileName): bool
    {
        if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
            // On Windows, unlinking a locked/opened file will not work, so we just attempt the delete straight away.
            // In the worst case, the unlink will just fail due to concurrent access and the caller needs to deal with that.
            return unlink($fileName);
        }
        $file = fopen($fileName, 'rb');
        if ($file === false) {
            return false;
        }

        $result = false;
        if (flock($file, LOCK_EX) !== false) {
            $result = unlink($fileName);
            flock($file, LOCK_UN);
        }
        fclose($file);

        return $result;
    }
private function tryRemoveWithLock(string $fileName): bool
    {
        if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
            // On Windows, unlinking a locked/opened file will not work, so we just attempt the delete straight away.
            // In the worst case, the unlink will just fail due to concurrent access and the caller needs to deal with that.
            return unlink($fileName);
        }
        $file = fopen($fileName, 'rb');
        if ($file === false) {
            return false;
        }

        $result = false;
        if (flock($file, LOCK_EX) !== false) {
            $result = unlink($fileName);
            flock($file, LOCK_UN);
        }
        fclose($file);

        return $result;
    }
private function tryRemoveWithLock ( string $ fileName ) : bool { if ( strncasecmp ( PHP_OS , 'WIN' , 3 ) == 0 ) { // On Windows, unlinking a locked/opened file will not work, so we just attempt the delete straight away. // In the worst case, the unlink will just fail due to concurrent access and the caller needs to deal with that. return unlink ( $ fileName ) ; } $ file = fopen ( $ fileName , 'rb' ) ; if ( $ file === false ) { return false ; } $ result = false ; if ( flock ( $ file , LOCK_EX ) !== false ) { $ result = unlink ( $ fileName ) ; flock ( $ file , LOCK_UN ) ; } fclose ( $ file ) ; return $ result ; }
Try to remove a file and make sure it is not locked.

@param string $fileName The full filename of the file to remove.
@return bool True if the file was removed successfully or false otherwise
Try to remove a file and make sure it is not locked .