What follows is kind of complicated. You can probably just use
file_get_contents without any issues.
There's the filesystem api. Which deals with determining ownership of a file and returning the correct way of accessing it -- most of the time that just means normal PHP functions (or the direct filesystem class).
Otto has a nice tutorial on this which I'm copied/altered to figure out how it works for this answer.
Essentially you use
request_filesystem_credentials to get the appropriate information to interact with the file system. This might be nothing (eg. direct, see the link above) or it may show a form to the user so they can enter the username/password for FTP or SSH. Depends on your server setup.
Some quick examples. Let's add an admin page, with a simple form.
<?php screen_icon(); ?>
<h2><?php _e('Filesystem Practice', 'wpse'); ?></h2>
<form method="post" action="">
<?php wp_nonce_field('wpse74395-nonce'); ?>
<p><input name="save" type="submit" value="<?php esc_attr_e('Go!', 'wpse'); ?>" class="button-primary" /></p>
// can we do this?
// try to get the filesystem credentials.
$url = wp_nonce_url(admin_url('options-general.php?page=wpse-filesystem'), 'wpse74395-nonce');
if(false === ($creds = request_filesystem_credentials($url)))
// we didn't get creds...
// The user will see a form at this point, so stop the
// rest of the page.
// use WP_Filesystem to check initialize the global $wp_filesystem
// didn't work, try again!
// get your file path
$fp = WP_CONTENT_DIR . '/test.txt';
// do your thing.
$res = $wp_filesystem->get_contents($fp);
// nothing worked, try again.
The comments should help explain what's going on. Essentially: make sure we're in a POST request (form submission), and the request checks out (nonce validation). From there, we'll try to get filesystem credentials. If we don't get any, a form will show to collect them, so stop the rest of the page from showing.
By only giving
request_filesystem_credentials the first argument, we're letting WP decide what method we need to use. Most of the time this is direct and the user won't see anything. Please note that
request_filesystem_credentials will do everything it can to get the stuff without asking the user -- database options, config options, etc.
Once we have the creds, try to initialize the global
$wp_filesystem object with
WP_Filesystem. If it works, awesome! If not, try to get the credentials again.
If everything checks out, use the
$wp_filesystem as you need to. They all have a consistent interface. Here's the above code in a plugin.