1. Home
  2. Computing & Technology
  3. PHP / MySQL

Read & Return Data From Files Into PHP

By , About.com Guide

2 of 5

Using fread () in PHP

You can use fread () when you want to read the file data and return it to a string. You can specify how many bytes to read, but 8192 is the maximum. If you choose to use this method, you must be sure to open the file first.
<?php
$YourFile = "YourFile.txt";
$handle = fopen($YourFile, 'r');
$Data = fread($handle, 512);
fclose($handle);
print $Data;
?>
This code opens the file YourFile.txt from your server and puts it's entire contents into the variable $Data as a string. We then print $Data, so the script is basically outputting the file contents onto a page.
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Advanced PHP
  5. fread - fread php - fread tutorial

©2009 About.com, a part of The New York Times Company.

All rights reserved.