Getting the Contents of a File

Problem

You want to load the contents of a file.

Solution

Use the File::get() method.

$contents = File::get($filename);

Discussion

If the file isn't found an exception is returned.

Specifically, a Illuminate\Filesystem\FileNotFoundException exception is returned.

If you don't explicitly test if the file exists yourself, it's good practice to wrap the call in a try/catch block.

try
{
    $contents = File::get($filename);
}
catch (Illuminate\Filesystem\FileNotFoundException $exception)
{
    die("The file doesn't exist");
}
comments powered by Disqus