-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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