Read contents of a file with php

The below code snippet gets the contents of a file into a string using php. fread() reads up to length bytes from the file pointer referenced by handle. Reading stops when length bytes have been read.

// get contents of a file into a string
$filename = '/path/to/file';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

get file to string

$string = file_get_contents($file);