<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.fgets.php',
    1 => 'fgets',
    2 => 'Prende una riga da un puntatore a file',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.fgetcsv.php',
    1 => 'fgetcsv',
  ),
  'next' => 
  array (
    0 => 'function.fgetss.php',
    1 => 'fgetss',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'it',
    'path' => 'reference/filesystem/functions/fgets.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.fgets" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">fgets</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fgets</span> &mdash; <span class="dc-title">Prende una riga da un puntatore a file</span></p>

   </div>
   <div class="refsect1 unknown-559" id="refsect1-function.fgets-unknown-559">
    <h3 class="title">Descrizione</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>fgets</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$handle</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$length</code></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

    <p class="para rdfs-comment">
     Restituisce una stringa di <code class="parameter">length</code> - 1 byte letti dal file puntato da <code class="parameter">handle</code>.  
     La lettura termina quando sono stati letti <code class="parameter">length</code> - 1 byte, oppure si incontra il carattere
     di newline (che viene incluso nel valore restituito), oppure alla fine del file
     (EOF) qualora giunga prima. Se non si specifica length, si assume
     come default 1k, o 1024 byte.
    </p>
    <p class="para">
     Se si verifica un errore, la funzione restituisce <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
    </p>
    <p class="para">
     Errori comuni:
    </p>
    <p class="simpara">
     Le persone abituate alla semantica &#039;C&#039; di fgets notino la differenza
     nel trattamento dell&#039;EOF.
    </p>

    <p class="para">Il puntatore al file deve essere valido, e deve puntare ad
un file aperto con successo da <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> o
<span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> (e non ancora chiuso da
<span class="function"><a href="function.fclose.php" class="function">fclose()</a></span>).</p>

    <p class="para">
     Segue un semplice esempio:
     <div class="example" id="example-1"> 
      <p><strong>Example #1 Legge un file riga per riga</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/inputfile.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">)) {<br />    </span><span style="color: #0000BB">$buffer </span><span style="color: #007700">= </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fd</span><span style="color: #007700">, </span><span style="color: #0000BB">4096</span><span style="color: #007700">);<br />    echo </span><span style="color: #0000BB">$buffer</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
    <blockquote class="note"><p><strong class="note">Nota</strong>: 
     <span class="simpara">
      Il parametro <code class="parameter">length</code> è diventato opzionale a partire da PHP
      4.2.0, se omesso, si assume come lunghezza della linea 1024.
      A partire dalla versione 4.3, l&#039;omissione del parametro <code class="parameter">length</code> 
      comporta la lettura del flusso d&#039;ingresso sino al raggiungimento della fine della linea.
      Se la maggior parte delle righe lette dal file hanno dimensione superiore a 8KB,
      è più efficiente specificare la lunghezza massima 
      della linea.
     </span>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Nota</strong>: 
     <span class="simpara">
      A partire da PHP 4.3 questa funzione è &#039;binary safe&#039;. Le versioni
      precedenti non lo sono.
     </span>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Nota</strong>: <span class="simpara">Se PHP non riconosce
correttamente i fine riga durante la lettura di file creati o provenienti
da un computer Macintosh, l&#039;abilitazione dell&#039;opzione di configurazione runtime
<a href="filesystem.configuration.php#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
potrebbe aiutare a risolvere il problema.</span></p></blockquote>
    <p class="para"> 
     Vedere anche <span class="function"><a href="function.fread.php" class="function">fread()</a></span>, 
     <span class="function"><a href="function.fgetc.php" class="function">fgetc()</a></span>,
     <span class="function"><a href="function.stream-get-line.php" class="function">stream_get_line()</a></span>,
     <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>,
     <span class="function"><a href="function.popen.php" class="function">popen()</a></span>, 
     <span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> e
     <span class="function"><a href="function.stream-set-timeout.php" class="function">stream_set_timeout()</a></span>.
    </p>
   </div>

  </div><?php manual_footer($setup); ?>