PHP stream_supports_lock() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_supports_lock() reports whether an open stream supports locking with flock().

File streams usually return true. Memory streams, some network sockets, and certain wrappers return false. Check before calling flock() when your code must run on many stream types.

Syntax

Call stream_supports_lock() with the values you need. Start with this line.

PHP
$canLock = stream_supports_lock($stream);
CallWhat it does
stream_supports_lock($file)Returns true for a normal file stream that accepts flock().
stream_supports_lock($memory)Often false for php://memory or php://temp depending on use.
stream_supports_lock($stream) && flock($stream, LOCK_EX)Locks only when the stream supports it.

This function only answers "can I flock?". It does not lock anything by itself. Always open the stream first, then test, then call flock() if needed.

Library declarations
PHP
stream_supports_lock(resource $stream): bool

Parameters

ParameterDescription
streamRequired. An open stream resource to inspect.

Return Value

Return ValueDescription
trueflock() is supported on this stream.
falseflock() is not supported on this stream.

PHP Examples

Example 1 — Check a Real File Stream

Open a temp file and ask whether locking is supported.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_lock.txt';
$stream = fopen($path, 'c+');

echo stream_supports_lock($stream) ? 'lock ok' : 'no lock', PHP_EOL;

fclose($stream);
unlink($path);

How It Works

  1. fopen() opens a real file.
  2. stream_supports_lock() returns true for typical files.
  3. You may then call flock() safely.
  4. Clean up the demo file.

Example 2 — Compare File and Memory Streams

Show that a file supports locking while php://memory often does not.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_lock2.txt';
$file = fopen($path, 'c+');
$mem = fopen('php://memory', 'r+');

echo 'file=', stream_supports_lock($file) ? 'yes' : 'no', PHP_EOL;
echo 'memory=', stream_supports_lock($mem) ? 'yes' : 'no', PHP_EOL;

fclose($file);
fclose($mem);
unlink($path);

How It Works

  1. A disk file stream usually supports flock().
  2. php://memory is not a lockable file.
  3. The boolean results guide whether locking makes sense.
  4. Close both streams afterward.

Example 3 — Guard Before flock()

Only attempt an exclusive lock when the stream supports it.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_lock3.txt';
$stream = fopen($path, 'c+');

if (stream_supports_lock($stream)) {
    flock($stream, LOCK_EX);
    fwrite($stream, "safe\n");
    flock($stream, LOCK_UN);
    echo 'wrote with lock', PHP_EOL;
} else {
    echo 'skipped lock', PHP_EOL;
}

fclose($stream);
unlink($path);

How It Works

  1. Test support before locking.
  2. LOCK_EX takes an exclusive lock.
  3. Write while holding the lock, then LOCK_UN.
  4. This pattern is safe in reusable helpers.

Frequently Asked Questions

It returns true when flock() can be used on the given stream, and false otherwise.
No. It only checks support. Call flock() yourself when you need a lock.
Ordinary file streams opened with fopen() on a real path.
Many memory streams, sockets, and some remote or custom wrappers.
Calling flock() on an unsupported stream fails. Checking first keeps libraries portable.

Next: stream_wrapper_register()

stream_wrapper_register() registers a custom protocol scheme handled by a user-defined str.

stream_wrapper_register() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful