29
29
use function is_array ;
30
30
use function is_string ;
31
31
use function stream_get_contents ;
32
+ use function stream_get_meta_data ;
32
33
use function strlen ;
33
34
use function trim ;
34
35
@@ -304,7 +305,7 @@ public static function streamOpen(string $path)
304
305
*/
305
306
public static function streamWrite ($ stream , string $ content , string $ path = '' ): void
306
307
{
307
- self ::assertWritableStream ($ stream );
308
+ // self::assertWritableStream($stream);
308
309
309
310
if (($ result = fwrite ($ stream , $ content )) === false || ($ result < strlen ($ content ))) {
310
311
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
336
337
return trim ((string )fgets ($ stream ));
337
338
}
338
339
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
+
339
353
/**
340
354
* @param resource $stream
341
355
* @param int $length
@@ -350,17 +364,48 @@ public static function streamReadAll($stream, int $length = -1, int $offset = -1
350
364
return (string )stream_get_contents ($ stream , $ length , $ offset );
351
365
}
352
366
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
+
353
398
/**
354
399
* ********************** 创建多级目录和多个文件 **********************
355
400
* 结合上两个函数
356
401
*
357
402
* @param array $fileData - 数组:要创建的多个文件名组成,含文件的完整路径
358
403
* @param bool $append - 是否以追加的方式写入数据 默认false
359
404
* @param int $mode =0777 - 权限,默认0775
360
- * eg: $fileData = array(
405
+ * eg: $fileData = [
361
406
* 'file_name' => 'content',
362
407
* 'case.html' => 'content' ,
363
- * ) ;
408
+ * ] ;
364
409
**/
365
410
public static function createAndWrite (array $ fileData = [], bool $ append = false , int $ mode = 0664 ): void
366
411
{
0 commit comments