back       next

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));
fclose($handle);

Post new comment

  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.