Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created February 25, 2015 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/0879a5a6ab6d6cffdd3d to your computer and use it in GitHub Desktop.
Save tommcfarlin/0879a5a6ab6d6cffdd3d to your computer and use it in GitHub Desktop.
[PHP] A simple example for how to use fread and related methods for reading the contents of a file.
<?php
/**
* Retrieves the contents of a file at the specified filename.
*
* @since 1.0.0
*
* @param string $filename The path to the file to read.
* @return string The entire contents of the file.
*/
function acme_read_file( $filename ) {
// Check to see if the file exists at the specified path
if ( ! file_exists( $filename ) ) {
throw new Exception( "The file doesn't exist." );
}
// Open the file for reading
$file_resource = fopen( $filename, 'r' );
/* Read the entire contents of the file which is indicated by
* the filesize argument
*/
$content = fread( $file_resource, filesize( $filename ) );
fclose( $file_resource );
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment