Skip to content

Commit 874be92

Browse files
committed
add smoe new methods
1 parent 9c46c9b commit 874be92

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/File.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use function is_array;
3030
use function is_string;
3131
use function stream_get_contents;
32+
use function stream_get_meta_data;
3233
use function strlen;
3334
use function trim;
3435

@@ -304,7 +305,7 @@ public static function streamOpen(string $path)
304305
*/
305306
public static function streamWrite($stream, string $content, string $path = ''): void
306307
{
307-
self::assertWritableStream($stream);
308+
// self::assertWritableStream($stream);
308309

309310
if (($result = fwrite($stream, $content)) === false || ($result < strlen($content))) {
310311
throw new IOException('The file "' . $path . '" could not be written to. Check your disk space and file permissions.');
@@ -336,6 +337,19 @@ public static function streamReadln($stream): string
336337
return trim((string)fgets($stream));
337338
}
338339

340+
/**
341+
* Gets line from file pointer
342+
*
343+
* @param resource $stream
344+
* @param int|null $length
345+
*
346+
* @return string
347+
*/
348+
public static function streamFgets($stream, ?int $length = null): string
349+
{
350+
return trim((string)fgets($stream, $length));
351+
}
352+
339353
/**
340354
* @param resource $stream
341355
* @param int $length
@@ -350,17 +364,48 @@ public static function streamReadAll($stream, int $length = -1, int $offset = -1
350364
return (string)stream_get_contents($stream, $length, $offset);
351365
}
352366

367+
/**
368+
* Retrieves header/meta data from streams/file pointers
369+
*
370+
* Returns:
371+
*
372+
* ```php
373+
* [
374+
* "timed_out" => "bool",
375+
* "blocked" => "bool",
376+
* "eof" => "bool",
377+
* "unread_bytes" => "int",
378+
* "stream_type" => "string",
379+
* "wrapper_type" => "string",
380+
* "wrapper_data" => "mixed",
381+
* "mode" => "string",
382+
* "seekable" => "bool",
383+
* "uri" => "string",
384+
* "crypto" => "array",
385+
* "mediatype" => "string",
386+
* ]
387+
* ```
388+
*
389+
* @param $stream
390+
*
391+
* @return array
392+
*/
393+
public static function streamMetadata($stream): array
394+
{
395+
return stream_get_meta_data($stream);
396+
}
397+
353398
/**
354399
* ********************** 创建多级目录和多个文件 **********************
355400
* 结合上两个函数
356401
*
357402
* @param array $fileData - 数组:要创建的多个文件名组成,含文件的完整路径
358403
* @param bool $append - 是否以追加的方式写入数据 默认false
359404
* @param int $mode =0777 - 权限,默认0775
360-
* eg: $fileData = array(
405+
* eg: $fileData = [
361406
* 'file_name' => 'content',
362407
* 'case.html' => 'content' ,
363-
* );
408+
* ];
364409
**/
365410
public static function createAndWrite(array $fileData = [], bool $append = false, int $mode = 0664): void
366411
{

0 commit comments

Comments
 (0)