Skip to content

Files and Streams

Dmitriy Zayceff edited this page Apr 29, 2015 · 11 revisions

Zend PHP has a few functions for working with files (or streams), these are named like in the C language: fopen, fclose, fwrite, etc. JPHP does not implement this functions. You should use an alternative approach in JPHP - php\io\Stream class and its chlidren.

use php\io\Stream;

$stream = Stream::of('path/to/file', 'r');
// or
$stream = new FileStream('path/to/file', 'r');

try {
   $str = $stream->read(10);
   // or read fully
   $str = $stream->readFully();
} finally {
   $stream->close();
}

// instead of ...

$fp = fopen('path/to/file', 'r');
$str = fread($fp, 10);
fclose($fp);

You still can use util class methos of Stream:

$str = Stream::getContents('path/to/file');

// instead of
$str = file_get_contents('path/to/tile');
  • file_get_contents -> Stream::getContents
  • file_put_contents -> Stream::putContents
  • file_exists -> Stream::exists, works with not only files or use php\io\File::exists for files only
  • fopen, fclose, fwrite, fread, fseek, etc. -> Stream classes
Clone this wiki locally