Oihana PHP

getFileContent.php

Table of Contents

Functions

getFileContent()  : string
Reads the entire contents of a file into a string.

Functions

getFileContent()

Reads the entire contents of a file into a string.

getFileContent(string|null $file[, int|null $maxBytes = null ]) : string

Symmetric counterpart of makeFile() (which writes a file): where getFileLines() reads a file line by line, this helper returns the whole content as a single string.

Parameters
$file : string|null

The full path to the file to read.

$maxBytes : int|null = null

Optional cap on the file size (in bytes). When set, the file is rejected before* being read if its size exceeds this value, throwing RuntimeException. Default null (no limit). Useful as a defensive guard against OOM when the caller does not fully trust the input size.

Tags
throws
FileException

If the file does not exist, is not readable, or cannot be read.

RuntimeException

If the file size exceeds $maxBytes.

author

Marc Alcaraz (ekameleon)

since
1.2.0
example
use function oihana\files\getFileContent;

$content = getFileContent( '/path/to/config.json' ) ;

// Refusing files larger than 10 MiB (defensive cap on untrusted sources).
$content = getFileContent( '/path/to/upload.bin' , 10 * 1024 * 1024 ) ;
Return values
string

The full file contents. An empty string for an empty file.

On this page

Search results