title: SplStream meta:

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

SplStream

Use

Resource flow data operation

How to operate

Method name parameter Description
__construct $resource = ‘’,$mode = ‘r+’ Initialize resources and read and write operations
__toString Output resource
close Close an open file pointer
detach Get resources and reset resource objects
getSize Get resource size Code conversion
tell Returns the location of the file pointer read/write
eof Whether the file pointer has reached the end of the file
isSeekable Get can be positioned in the current stream
seek $offset, $whence = SEEK_SET Positioning in the file pointer
rewind Rewind the position of the file pointer
isWritable Is it writable
write $string Write content
isReadable Readable
read $length Reading content
length Get the length of the string
getContents Read resource stream to a string
getMetadata $key = null Get header/metadata from the package protocol file pointer
getStreamResource Access to resources
truncate $size = 0 Truncate the file to the given length

example

__construct

Initialize resources and read and write operations

  • mixed $resource resource
  • mixed $mode read and write operation type
    1. function __construct($resource = '',$mode = 'r+')

::: 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. $resource = fopen('./test.txt', 'ab+');
  10. $stream = new \EasySwoole\Spl\SplStream($resource);
  11. var_dump($stream->__toString());
  12. /**
  13. * The output is over:
  14. * string(10) "Easyswoole"
  15. */

__toString

Output resource

  1. public function __toString()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. var_dump($stream->__toString());
  11. /**
  12. * The output is over:
  13. * string(10) "Easyswoole"
  14. */

close

Close an open file pointer

  1. public function close()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $stream->close();
  11. var_dump($stream->__toString());
  12. /**
  13. * The output is over:
  14. * string(0) ""
  15. */

detach

Get resources and reset resource objects

  1. public function detach()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $stream->detach();
  11. var_dump($stream->__toString());
  12. /**
  13. * The output is over:
  14. * string(0) ""
  15. */

getSize

Get resource size

  1. public function getSize()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $size = $stream->getSize();
  11. var_dump($size);
  12. /**
  13. * The output is over:
  14. * int(10)
  15. */

tell

Returns the location of the file pointer read/write

  1. public function tell()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $position = $stream->tell();
  11. var_dump($position);
  12. /**
  13. * The output is over:
  14. * int(10)
  15. */

eof

Whether the file pointer has reached the end of the file

  1. public function eof()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $eof = $stream->eof();
  11. var_dump($eof);
  12. $stream->detach();
  13. $eof = $stream->eof();
  14. var_dump($eof);
  15. /**
  16. * The output is over:
  17. * bool(false)
  18. * bool(true)
  19. */

isSeekable

Get can be positioned in the current stream

  1. public function isSeekable()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $seekable = $stream->isSeekable();
  11. var_dump($seekable);
  12. /**
  13. * The output is over:
  14. * bool(true)
  15. */

seek

Positioning in the file pointer

  • mixed $offset offset
  • mixed $whence specified type

    1. public function seek($offset, $whence = SEEK_SET)

    Specified type:

  • SEEK_SET set position equal to offset byte

  • SEEK_CUR sets the position to the current position plus offset
  • SEEK_END sets the position to the end of the file plus offset

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $stream->seek(2);
  11. $position = $stream->tell();
  12. var_dump($position);
  13. /**
  14. * The output is over:
  15. * int(2)
  16. */

rewind

Rewind the position of the file pointer

  1. public function rewind()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $stream->rewind();
  11. $position = $stream->tell();
  12. var_dump($position);
  13. /**
  14. * The output is over:
  15. * int(0)
  16. */

isWritable

Is it writable

  1. public function isWritable()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $writeAble = $stream->isWritable();
  11. var_dump($writeAble);
  12. /**
  13. * The output is over:
  14. * bool(true)
  15. */

write

Write content

  • mixed $string content
    1. public function write($string)

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $stream->write(', 666');
  11. var_dump($stream->__toString());
  12. /**
  13. * The output is over:
  14. * string(15) "Easyswoole, 666"
  15. */

isReadable

Whether to read

  1. public function isReadable()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $readAble = $stream->isReadable();
  11. var_dump($readAble);
  12. /**
  13. * The output is over:
  14. * bool(true)
  15. */

read

Reading content

  • mixed $length length
    1. public function read($length)

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $stream->rewind();
  11. $string = $stream->read(4);
  12. var_dump($string);
  13. /**
  14. * The output is over:
  15. * string(4) "Easy"
  16. */

getContents

Read resource stream to a string

  1. public function getContents()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $stream->rewind();
  11. $string = $stream->getContents();
  12. var_dump($string);
  13. /**
  14. * The output is over:
  15. * string(10) "Easyswoole"
  16. */

getMetadata

Get header/metadata from the package protocol file pointer

  1. public function getMetadata($key = null)

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $meta = $stream->getMetadata();
  11. var_dump($meta['stream_type']);
  12. /**
  13. * The output is over:
  14. * string(6) "MEMORY"
  15. */

getStreamResource

Access to resources

  1. function getStreamResource()

::: 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. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $resource = $stream->getStreamResource();
  11. fseek($resource, 0, SEEK_SET);
  12. var_dump(stream_get_contents($resource));
  13. /**
  14. * The output is over:
  15. * string(10) "Easyswoole"
  16. */

truncate

Truncate the file to the given length

  • mixed $size Intercept file size
    1. function truncate($size = 0)

::: warning example :::

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 19-7-2
  6. * Time: 10:25 am
  7. */
  8. require_once 'vendor/autoload.php';
  9. $stream = new \EasySwoole\Spl\SplStream('Easyswoole');
  10. $stream->truncate(4);
  11. var_dump($stream->__toString());
  12. /**
  13. * The output is over:
  14. * string(4) "Easy"
  15. */

::: warning Ps: There is a difference between resources and resource flows. The resources mentioned here are data or variables. The resource flow is a file stream. :::