<HTML ><HEAD ><TITLE >fread</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Filesystem functions" HREF="ref.filesystem.html"><LINK REL="PREVIOUS" TITLE="fputs" HREF="function.fputs.html"><LINK REL="NEXT" TITLE="fscanf" HREF="function.fscanf.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.fputs.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.fscanf.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.fread" ></A >fread</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN26030" ></A ><P > (PHP 3, PHP 4 )</P >fread -- Binary-safe file read</DIV ><DIV CLASS="refsect1" ><A NAME="AEN26033" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >fread</B > ( resource handle, int length)<BR ></BR ><P > <B CLASS="function" >fread()</B > reads up to <TT CLASS="parameter" ><I >length</I ></TT > bytes from the file pointer referenced by <TT CLASS="parameter" ><I >handle</I ></TT >. Reading stops when <TT CLASS="parameter" ><I >length</I ></TT > bytes have been read or EOF (end of file) reached, whichever comes first. </P ><P > <DIV CLASS="informalexample" ><A NAME="AEN26050" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen ($filename, "r"); $contents = fread ($handle, filesize ($filename)); fclose ($handle); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A > mode parameter. </P ></BLOCKQUOTE ></DIV ><P > <DIV CLASS="informalexample" ><A NAME="AEN26056" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $filename = "c:\\files\\somepic.gif"; $handle = fopen ($filename, "rb"); $contents = fread ($handle, filesize ($filename)); fclose ($handle); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > See also <A HREF="function.fwrite.html" ><B CLASS="function" >fwrite()</B ></A >, <A HREF="function.fopen.html" ><B CLASS="function" >fopen()</B ></A >, <A HREF="function.fsockopen.html" ><B CLASS="function" >fsockopen()</B ></A >, <A HREF="function.popen.html" ><B CLASS="function" >popen()</B ></A >, <A HREF="function.fgets.html" ><B CLASS="function" >fgets()</B ></A >, <A HREF="function.fgetss.html" ><B CLASS="function" >fgetss()</B ></A >, <A HREF="function.fscanf.html" ><B CLASS="function" >fscanf()</B ></A >, <A HREF="function.file.html" ><B CLASS="function" >file()</B ></A >, and <A HREF="function.fpassthru.html" ><B CLASS="function" >fpassthru()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.fputs.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.fscanf.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >fputs</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.filesystem.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >fscanf</TD ></TR ></TABLE ></DIV ></BODY ></HTML >