SplFileStream

Use

File resource stream data operation

How to operate

Method Name Parameters Description
__construct $file,$mode = 'c+' Initialize resources and read and write operations
lock $mode = LOCK_EX File Lock
unlock $mode = LOCK_UN Release Lock

The SplFileStream class inherits SplStream, and other related methods refer to SplStream

example

__construct

Initialize resources and read and write operations

  • mixed $file file
  • mixed $mode read and write operation type
function __construct($file,$mode = 'c+')

example

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 19-7-2
 * Time: 上午10:25
 */

require_once 'vendor/autoload.php';

$fileStream = new \EasySwoole\Spl\SplFileStream('./test.txt');
$type = $fileStream->getMetadata('stream_type');
var_dump($type);

/**
 * The output is over:
 * string(5) "STDIO"
 */

lock

File lock

  • mixed $mode lock type

Lock type:

  • LOCK_SH gets shared lock (read program)
  • LOCK_EX gets exclusive lock (written program)
  • LOCK_UN release lock (whether shared or exclusive)
    function lock($mode = LOCK_EX)

example

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 19-7-2
 * Time: 上午10:25
 */

require_once 'vendor/autoload.php';

$fileStream = new \EasySwoole\Spl\SplFileStream('./test.txt');
$lock = $fileStream->lock();
var_dump($lock);

/**
 * The output is over:
 * bool(true)
 */

unlock

Release lock

  • mixed $mode Lock type
    function unlock($mode = LOCK_UN)

example

<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 19-7-2
 * Time: 上午10:25
 */

require_once 'vendor/autoload.php';

$fileStream = new \EasySwoole\Spl\SplFileStream('./test.txt');
$unlock = $fileStream->unlock();
var_dump($unlock);

/**
 * The output is over:
 * bool(true)
 */