Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

fnmatch()

Example

Shut down and unlock a file:

<?php
$file = fopen(“test.txt”,“w+”);

// exclusive lock
if (flock($file,LOCK_EX)) {
  fwrite($file,“Add some text to the file.”);
  fflush($file);
  // release lock
  flock($file,LOCK_UN);
else {
  echo “Error locking file!”;
}
fclose($file);
?>

Definition and Usage

A file is locked and released by the flock() function.Reword

Syntax

flock(filelockblock)

Parameter Values

Parameter

Description

file

Essential. To lock or release an open file, specify it.

lock

Essential. indicates the type of lock to be used.

Potential amounts

·         LOCK_SH: A lock that is shared (reader). Permit file access to other processes

·         LOCK_EX: A writer lock that is exclusive. Stop other programs from opening the file.

·         LOCK_UN: Unlock the door

·         LOCK_NB: Don’t obstruct other processes when you’re locking

block

Not required. Set to 1 to lock and prevent other processes from opening.

Technical Details

Return Value:

FALSE when it fails, TRUE when it succeeds

PHP Version:

4.0+

PHP Changelog:

PHP 5.5: Windows users now have support for the block parameter
PHP 5.3: fclose() no longer automatically unlocks. Manual unlocking is now required.