title: SplFileStream meta:

  • name: description content: EasySwoole SplFileStream
  • name: keywords content: swoole|swoole extension|swoole framework|easyswoole,SplFileStream

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

::: warning 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
  1. function __construct($file,$mode = 'c+')

::: warning example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-7-2
  6. * Time: 上午10:25
  7. */
  8. require_once 'vendor/autoload.php';
  9. $fileStream = new \EasySwoole\Spl\SplFileStream('./test.txt');
  10. $type = $fileStream->getMetadata('stream_type');
  11. var_dump($type);
  12. /**
  13. * The output is over:
  14. * string(5) "STDIO"
  15. */

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)
    1. function lock($mode = LOCK_EX)

::: warning example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-7-2
  6. * Time: 上午10:25
  7. */
  8. require_once 'vendor/autoload.php';
  9. $fileStream = new \EasySwoole\Spl\SplFileStream('./test.txt');
  10. $lock = $fileStream->lock();
  11. var_dump($lock);
  12. /**
  13. * The output is over:
  14. * bool(true)
  15. */

unlock

Release lock

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

::: warning example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-7-2
  6. * Time: 上午10:25
  7. */
  8. require_once 'vendor/autoload.php';
  9. $fileStream = new \EasySwoole\Spl\SplFileStream('./test.txt');
  10. $unlock = $fileStream->unlock();
  11. var_dump($unlock);
  12. /**
  13. * The output is over:
  14. * bool(true)
  15. */