<?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.flock.php',
    1 => 'flock',
    2 => 'Sistema di bloccaggio file',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.filetype.php',
    1 => 'filetype',
  ),
  'next' => 
  array (
    0 => 'function.fnmatch.php',
    1 => 'fnmatch',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'it',
    'path' => 'reference/filesystem/functions/flock.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.flock" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">flock</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">flock</span> &mdash; <span class="dc-title">Sistema di bloccaggio file</span></p>

   </div>
   <div class="refsect1 unknown-595" id="refsect1-function.flock-unknown-595">
    <h3 class="title">Descrizione</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>flock</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">$operation</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter reference">&$wouldblock</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

    <p class="simpara">
     Il PHP supporta un tecnologia portabile per bloccare file completi in modalità
     advisory (tutti i programmi che vi accedono, devono usare lo stesso tipo di
     bloccaggio o non funzionerà).
    </p>
    <blockquote class="note"><p><strong class="note">Nota</strong>: 
     <p class="para">
      <span class="function"><strong>flock()</strong></span>è obbligatorio sotto Windows.
     </p>
    </p></blockquote>
    <p class="simpara">
     <span class="function"><strong>flock()</strong></span> opera su <code class="parameter">handle</code>
     che deve essere un puntatore ad un file aperto.
     <code class="parameter">operation</code> può assumere uno dei valori
     seguenti:
    </p>
    <p class="para">
     <ul class="itemizedlist">
      <li class="listitem">
       <span class="simpara">
         Per acquisire una chiave condivisa (in lettura), imposta
         <code class="parameter">operation</code> a <strong><code><a href="filesystem.constants.php#constant.lock-sh">LOCK_SH</a></code></strong> (usa 1 prima di
         PHP 4.0.1).
       </span>
      </li>
      <li class="listitem">
       <span class="simpara">
         Per acquisire una chiave esclusiva (in scrittura), imposta
         <code class="parameter">operation</code> a <strong><code><a href="filesystem.constants.php#constant.lock-ex">LOCK_EX</a></code></strong> (usa 2 prima di
         PHP 4.0.1).
       </span>
      </li>
      <li class="listitem">
       <span class="simpara">
         Per rilasciare una chiave (condivisa o esclusiva), imposta
         <code class="parameter">operation</code> a <strong><code><a href="filesystem.constants.php#constant.lock-un">LOCK_UN</a></code></strong> (usa 3 prima
         PHP 4.0.1).
       </span>
      </li>
      <li class="listitem">
       <span class="simpara">
        Se non vuoi che <span class="function"><strong>flock()</strong></span> blocchi mentre,
        imposta come <strong><code><a href="filesystem.constants.php#constant.lock-nb">LOCK_NB</a></code></strong> (4 prima di PHP 4.0.1)
        <code class="parameter">operation</code>.
       </span>
      </li>
     </ul>
    </p>
    <p class="simpara">
     <span class="function"><strong>flock()</strong></span> ti permette di utilizzare un semplice modello di
     lettura/scrittura che in teoria può essere usato su qualsiasi piattaforma
     (inclusi molti sistemi Unix e anche Windows).  Il terzo argomento (opzionale)
     può essere impostato a <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> se la chiave puo bloccare (EWOULDBLOCK
     errno condition). Il blocco è realizzato anche da <span class="function"><a href="function.fclose.php" class="function">fclose()</a></span>
     (che è anche richiamata automaticamente quando lo script termina).
    </p>
    <p class="simpara">
     Restituisce <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> in caso di successo, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> in caso di fallimento.
    </p>
    <p class="para">
     <div class="example" id="example-1">
      <p><strong>Example #1 Esempio di uso di <span class="function"><strong>flock()</strong></span></strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/lock.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w+"</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">LOCK_EX</span><span style="color: #007700">)) { </span><span style="color: #FF8000">// Esegue un lock esclusivo<br />    </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"Write something here\n"</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">LOCK_UN</span><span style="color: #007700">); </span><span style="color: #FF8000">// rilascia il lock<br /></span><span style="color: #007700">} else {<br />    echo </span><span style="color: #DD0000">"Non si riesce ad eseguire il lock del file !"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
      </div>

     </div>
    </p>
    <blockquote class="note"><p><strong class="note">Nota</strong>: 
     <p class="para">
      Poiché <span class="function"><strong>flock()</strong></span> richiede il puntatore ad un file, può occorre
      utilizzare un speciale file di lock per proteggere l&#039;accesso a eventuali file che si desidera
      azzerare attraverso l&#039;apertura in modalità di scrittura (usando &quot;w&quot; o &quot;w+&quot; come argomento
      di <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>).
     </p>
    </p></blockquote>
    <div class="warning"><strong class="warning">Avviso</strong>
     <p class="para">
      La funzione <span class="function"><strong>flock()</strong></span> non funzione con NFS e con diversi altri file system
      di rete. Verificare sulla documentazione del proprio sistema operativo.
     </p>
     <p class="para">
      Su molti sistemi operativi <span class="function"><strong>flock()</strong></span> è implementato a livello
      di processo. Usando un server API multithread quale
      ISAPI non potrai basarti su <span class="function"><strong>flock()</strong></span> per proteggere
      i file da altri script PHP che girino in thread paralleli della stessa istanza
      del server!
     </p>
     <p class="para">
      La funzione <span class="function"><strong>flock()</strong></span> non è supportata su file system antiquati tipo
      <code class="literal">FAT</code> e i suoi derivati e pertanto in tali ambienti restituirà sempre
      <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> (questo è vero soprattutto per gli utenti di
      Windows 98).
     </p>
    </div>
   </div>

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