Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 6380

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>fread</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Manual do PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="Filesystem (Sistema de arquivos)"
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=UTF-8"></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"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.fputs.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.fscanf.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.fread"
></A
>fread</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN49541"
></A
><P
>    (PHP 3, PHP 4, PHP 5)</P
>fread&nbsp;--&nbsp;Leitura binary-safe de arquivo</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN49544"
></A
><H2
>Descrição</H2
>string <B
CLASS="methodname"
>fread</B
> ( resource handle, int length )<BR
></BR
><P
>&#13;     <B
CLASS="function"
>fread()</B
> lê até
     <CODE
CLASS="parameter"
>length</CODE
> bytes do ponteiro de arquivo
     informado em <CODE
CLASS="parameter"
>handle</CODE
>. A leitura é interrompida quando
     <CODE
CLASS="parameter"
>length</CODE
> bytes foram lidos ou 
     o fim do arquivo (eof) foi alcançado, ou (para streams de rede) quando um
     pacote se torna disponível, o que ocorrer primeiro.
    </P
><P
>&#13;     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN49561"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// ler o conteúdo do arquivo para uma string<br /></font><font color="#0000BB">$filename </font><font color="#007700">= </font><font color="#DD0000">"/usr/local/qualquer.txt"</font><font color="#007700">;<br /></font><font color="#0000BB">$handle </font><font color="#007700">= </font><font color="#0000BB">fopen </font><font color="#007700">(</font><font color="#0000BB">$filename</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">);<br /></font><font color="#0000BB">$conteudo </font><font color="#007700">= </font><font color="#0000BB">fread </font><font color="#007700">(</font><font color="#0000BB">$handle</font><font color="#007700">, </font><font color="#0000BB">filesize </font><font color="#007700">(</font><font color="#0000BB">$filename</font><font color="#007700">));<br /></font><font color="#0000BB">fclose </font><font color="#007700">(</font><font color="#0000BB">$handle</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Atenção</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;      Em sistemas que diferenciam entre arquivos binários e texto
      (por exemplo Windows) o arquivo tem que ser aberto com 'b'
      incluído como parametro na função <A
HREF="function.fopen.html"
><B
CLASS="function"
>fopen()</B
></A
>.
     </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN49567"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$filename </font><font color="#007700">= </font><font color="#DD0000">"c:\\files\\figura.gif"</font><font color="#007700">;<br /></font><font color="#0000BB">$handle </font><font color="#007700">= </font><font color="#0000BB">fopen </font><font color="#007700">(</font><font color="#0000BB">$filename</font><font color="#007700">, </font><font color="#DD0000">"rb"</font><font color="#007700">);<br /></font><font color="#0000BB">$conteudo </font><font color="#007700">= </font><font color="#0000BB">fread </font><font color="#007700">(</font><font color="#0000BB">$handle</font><font color="#007700">, </font><font color="#0000BB">filesize </font><font color="#007700">(</font><font color="#0000BB">$filename</font><font color="#007700">));<br /></font><font color="#0000BB">fclose </font><font color="#007700">(</font><font color="#0000BB">$handle</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Atenção</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;      Quando lendo de streams de rede ou canais (pipes), como aqueles retornados pela
      leitura de <A
HREF="features.remote-files.html"
>arquivos remotos</A
> ou de
      <A
HREF="function.popen.html"
><B
CLASS="function"
>popen()</B
></A
> e <A
HREF="function.proc-open.html"
><B
CLASS="function"
>proc_open()</B
></A
>, a leitura
      irá parar depois que um pacote se torna disponível. Isto significa que você
      deve juntar os dados em blocos como demonstrado no exemplo seguinte.
     </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN49575"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$handle </font><font color="#007700">= </font><font color="#0000BB">fopen </font><font color="#007700">(</font><font color="#DD0000">"http://www.example.com/"</font><font color="#007700">, </font><font color="#DD0000">"rb"</font><font color="#007700">);<br /></font><font color="#0000BB">$conteudo </font><font color="#007700">= </font><font color="#DD0000">""</font><font color="#007700">;<br />do {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">fread</font><font color="#007700">(</font><font color="#0000BB">$handle</font><font color="#007700">, </font><font color="#0000BB">8192</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$data</font><font color="#007700">) == </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$conteudo </font><font color="#007700">.= </font><font color="#0000BB">$data</font><font color="#007700">;<br />} while(</font><font color="#0000BB">true</font><font color="#007700">);<br /></font><font color="#0000BB">fclose </font><font color="#007700">(</font><font color="#0000BB">$handle</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
      O exemplo acima tem uma performance melhor que o jeito comum
      usando while(!<A
HREF="function.feof.html"
><B
CLASS="function"
>feof()</B
></A
>), porque ele economiza o overhead
      de uma chamada de função por iteração.
     </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
      Se você quer apenas pegar o conteúdo de um arquivo para uma string, utilize
      <A
HREF="function.file-get-contents.html"
><B
CLASS="function"
>file_get_contents()</B
></A
> que tem uma performance ainda melhor
      que o código acima.
     </P
></BLOCKQUOTE
></DIV
><P
>&#13;     Veja também <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
> e
     <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"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.fscanf.html"
ACCESSKEY="N"
>Próxima</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"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>fscanf</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>