<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.fopen.php',
    1 => 'fopen',
    2 => 'Opens file or URL',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Функції Файлової Системи',
  ),
  'prev' => 
  array (
    0 => 'function.fnmatch.php',
    1 => 'fnmatch',
  ),
  'next' => 
  array (
    0 => 'function.fpassthru.php',
    1 => 'fpassthru',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/filesystem/functions/fopen.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

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

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fopen-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fopen</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$filename</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$mode</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$use_include_path</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>fopen()</strong></span> binds a named resource, specified by
   <code class="parameter">filename</code>, to a stream. 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fopen-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       If <code class="parameter">filename</code> is of the form &quot;scheme://...&quot;, it
       is assumed to be a URL and PHP will search for a protocol handler
       (also known as a wrapper) for that scheme. If no wrappers for that
       protocol are registered, PHP will emit a notice to help you track
       potential problems in your script and then continue as though
       <code class="parameter">filename</code> specifies a regular file.
      </p>
      <p class="para">
       If PHP has decided that <code class="parameter">filename</code> specifies
       a local file, then it will try to open a stream on that file.
       The file must be accessible to PHP, so you need to ensure that
       the file access permissions allow this access.
       If you have enabled
       <a href="ini.core.php#ini.open-basedir" class="link">open_basedir</a> further
       restrictions may apply.
      </p>
      <p class="para">
       If PHP has decided that <code class="parameter">filename</code> specifies
       a registered protocol, and that protocol is registered as a
       network URL, PHP will check to make sure that
       <a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">allow_url_fopen</a> is
       enabled. If it is switched off, PHP will emit a warning and
       the <span class="function"><strong>fopen()</strong></span> call will fail.
      </p>
      <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
       <p class="para">
        The list of supported protocols can be found in <a href="wrappers.php" class="xref">Підтримувані протоколи та обгортки</a>. Some protocols (also referred to as
        <code class="literal">wrappers</code>) support <code class="literal">context</code>
        and/or <var class="filename">php.ini</var> options. Refer to the specific page for the
        protocol in use for a list of options which can be set. (e.g.
        <var class="filename">php.ini</var> value <code class="literal">user_agent</code> used by the
        <code class="literal">http</code> wrapper).
       </p>
      </p></blockquote>
      <p class="para">
       On the Windows platform, be careful to escape any backslashes
       used in the path to the file, or use forward slashes.
       <div class="informalexample">
        <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">"c:\\folder\\resource.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
        </div>

       </div>
      </p>
     </dd>
    
    
     <dt><code class="parameter">mode</code></dt>
     <dd>
      <p class="para">
       The <code class="parameter">mode</code> parameter specifies the type of access
       you require to the stream.  It may be any of the following:
       <table class="doctable table">
        <caption><strong>
         A list of possible modes for <span class="function"><strong>fopen()</strong></span>
         using <code class="parameter">mode</code>
        </strong></caption>
        
         <thead>
          <tr>
           <th><code class="parameter">mode</code></th>
           <th>Description</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td><code class="literal">&#039;r&#039;</code></td>
           <td>
            Open for reading only; place the file pointer at the
            beginning of the file.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;r+&#039;</code></td>
           <td>
            Open for reading and writing; place the file pointer at
            the beginning of the file.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;w&#039;</code></td>
           <td>
            Open for writing only; place the file pointer at the
            beginning of the file and truncate the file to zero length.
            If the file does not exist, attempt to create it.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;w+&#039;</code></td>
           <td>
            Open for reading and writing; otherwise it has the
            same behavior as <code class="literal">&#039;w&#039;</code>.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;a&#039;</code></td>
           <td>
            Open for writing only; place the file pointer at the end of
            the file. If the file does not exist, attempt to create it.
            In this mode, <span class="function"><a href="function.fseek.php" class="function">fseek()</a></span> has no effect, writes are always appended.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;a+&#039;</code></td>
           <td>
            Open for reading and writing; place the file pointer at
            the end of the file. If the file does not exist, attempt to
            create it. In this mode, <span class="function"><a href="function.fseek.php" class="function">fseek()</a></span> only affects
            the reading position, writes are always appended.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;x&#039;</code></td>
           <td>
            Create and open for writing only; place the file pointer at the
            beginning of the file.  If the file already exists, the
            <span class="function"><strong>fopen()</strong></span> call will fail by returning <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> and
            generating an error of level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.  If
            the file does not exist, attempt to create it.  This is equivalent
            to specifying <code class="literal">O_EXCL|O_CREAT</code> flags for the
            underlying <code class="literal">open(2)</code> system call. 
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;x+&#039;</code></td>
           <td>
            Create and open for reading and writing; otherwise it has the
            same behavior as <code class="literal">&#039;x&#039;</code>.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;c&#039;</code></td>
           <td>
            Open the file for writing only. If the file does not exist, it is
            created. If it exists, it is neither truncated (as opposed to
            <code class="literal">&#039;w&#039;</code>), nor the call to this function fails (as is
            the case with <code class="literal">&#039;x&#039;</code>). The file pointer is
            positioned on the beginning of the file. This may be useful if it&#039;s
            desired to get an advisory lock (see <span class="function"><a href="function.flock.php" class="function">flock()</a></span>)
            before attempting to modify the file, as using
            <code class="literal">&#039;w&#039;</code> could truncate the file before the lock
            was obtained (if truncation is desired,
            <span class="function"><a href="function.ftruncate.php" class="function">ftruncate()</a></span> can be used after the lock is
            requested).
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;c+&#039;</code></td>
           <td>
            Open the file for reading and writing; otherwise it has the same
            behavior as <code class="literal">&#039;c&#039;</code>.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;e&#039;</code></td>
           <td>
            Set close-on-exec flag on the opened file descriptor. Only
            available in PHP compiled on POSIX.1-2008 conformant systems.
           </td>
          </tr>

          <tr>
           <td><code class="literal">&#039;n&#039;</code></td>
           <td>
            Set non-blocking flag on the opened file descriptor. Only
            available in PHP compiled on POSIX.1-2008 conformant systems.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
       <p class="para">
        Different operating system families have different line-ending
        conventions.  When you write a text file and want to insert a line
        break, you need to use the correct line-ending character(s) for your
        operating system.  Unix based systems use <code class="literal">\n</code> as the
        line ending character, Windows based systems use <code class="literal">\r\n</code>
        as the line ending characters and Macintosh based systems (Mac OS Classic) used
        <code class="literal">\r</code> as the line ending character.
       </p>
       <p class="para">
        If you use the wrong line ending characters when writing your files, you
        might find that other applications that open those files will &quot;look
        funny&quot;.
       </p>
       <p class="para">
        Windows offers a text-mode translation flag (<code class="literal">&#039;t&#039;</code>)
        which will transparently translate <code class="literal">\n</code> to
        <code class="literal">\r\n</code> when working with the file.  In contrast, you
        can also use <code class="literal">&#039;b&#039;</code> to force binary mode, which will not
        translate your data.  To use these flags, specify either
        <code class="literal">&#039;b&#039;</code> or <code class="literal">&#039;t&#039;</code> as the last character
        of the <code class="parameter">mode</code> parameter.
       </p>
       <p class="para">
        The default translation mode is <code class="literal">&#039;b&#039;</code>.
        You can use the <code class="literal">&#039;t&#039;</code>
        mode if you are working with plain-text files and you use
        <code class="literal">\n</code> to delimit your line endings in your script, but
        expect your files to be readable with applications such as old versions of notepad.  You
        should use the <code class="literal">&#039;b&#039;</code> in all other cases.
       </p>
       <p class="para">
        If you specify the &#039;t&#039; flag when working with binary files, you
        may experience strange problems with your data, including broken image
        files and strange problems with <code class="literal">\r\n</code> characters.
       </p>
      </p></blockquote>
      <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
       <p class="para">
        For portability, it is also strongly recommended that
        you re-write code that uses or relies upon the <code class="literal">&#039;t&#039;</code>
        mode so that it uses the correct line endings and
        <code class="literal">&#039;b&#039;</code> mode instead.
       </p>
      </p></blockquote>
      <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
       <span class="simpara">
        The <code class="parameter">mode</code> is ignored for <var class="filename">php://output</var>,
        <var class="filename">php://input</var>, <var class="filename">php://stdin</var>,
        <var class="filename">php://stdout</var>, <var class="filename">php://stderr</var> and
        <var class="filename">php://fd</var> stream wrappers.
       </span>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">use_include_path</code></dt>
     <dd>
      <p class="para">
       The optional third <code class="parameter">use_include_path</code> parameter
       can be set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if you want to search for the file in the
       <a href="ini.core.php#ini.include-path" class="link">include_path</a>, too.
      </p>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>
<a href="stream.contexts.php" class="link">контекст потоку</a>.</p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fopen-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Returns a file pointer resource on success,
    або <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> в разі помилки
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.fopen-errors">
  <h3 class="title">Помилки/виключення</h3>
  <p class="para">
Під час збоїв буде видаватись <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
</p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fopen-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.0.16, 7.1.2</td>
       <td>
        The <code class="literal">&#039;e&#039;</code> option was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fopen-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 <span class="function"><strong>fopen()</strong></span> examples</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">"/home/rasmus/file.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/home/rasmus/file.gif"</span><span style="color: #007700">, </span><span style="color: #DD0000">"wb"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"http://www.example.com/"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"ftp://user:password@example.com/somefile.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.fopen-notes">
  <h3 class="title">Примітки</h3>
  <div class="warning"><strong class="warning">Увага</strong><p class="para">Якщо використовувати SSL,
Microsoft IIS порушуватиме протокол, закриваючи зʼєднання без надсилання
індикатора <code class="literal">close_notify</code>. Коли буде досягнуто кінця даних, PHP
повідомлятиме: &quot;SSL: Fatal Protocol Error&quot;. Щоб обійти це, значення <a href="errorfunc.configuration.php#ini.error-reporting" class="link">error_reporting</a> має бути знижене до рівня,
який не включає попереджень. PHP може виявити несправне програмне забезпечення
сервера IIS та приховати попередження, якщо відкривати потік за допомогою
обгортки <code class="literal">https://</code>. Якщо розробник використовує
<span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> для створення сокета <code class="literal">ssl://</code>,
то він сам має виявляти та приховувати це попередження.</p></div>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    If you are experiencing problems with reading and writing to files and
    you&#039;re using the server module version of PHP, remember to make sure that
    the files and directories you&#039;re using are accessible to the server
    process.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    This function may also succeed when <code class="parameter">filename</code> is a
    directory. If you are unsure whether <code class="parameter">filename</code> is a
    file or a directory, you may need to use the <span class="function"><a href="function.is-dir.php" class="function">is_dir()</a></span>
    function before calling <span class="function"><strong>fopen()</strong></span>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.fopen-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="wrappers.php" class="xref">Підтримувані протоколи та обгортки</a></li>
    <li><span class="function"><a href="function.fclose.php" class="function" rel="rdfs-seeAlso">fclose()</a> - Closes an open file pointer</span></li>
    <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - Gets line from file pointer</span></li>
    <li><span class="function"><a href="function.fread.php" class="function" rel="rdfs-seeAlso">fread()</a> - Binary-safe file read</span></li>
    <li><span class="function"><a href="function.fwrite.php" class="function" rel="rdfs-seeAlso">fwrite()</a> - Binary-safe file write</span></li>
    <li><span class="function"><a href="function.fsockopen.php" class="function" rel="rdfs-seeAlso">fsockopen()</a> - Open Internet or Unix domain socket connection</span></li>
    <li><span class="function"><a href="function.file.php" class="function" rel="rdfs-seeAlso">file()</a> - Reads entire file into an array</span></li>
    <li><span class="function"><a href="function.file-exists.php" class="function" rel="rdfs-seeAlso">file_exists()</a> - Checks whether a file or directory exists</span></li>
    <li><span class="function"><a href="function.is-readable.php" class="function" rel="rdfs-seeAlso">is_readable()</a> - Tells whether a file exists and is readable</span></li>
    <li><span class="function"><a href="function.stream-set-timeout.php" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
    <li><span class="function"><a href="function.popen.php" class="function" rel="rdfs-seeAlso">popen()</a> - Opens process file pointer</span></li>
    <li><span class="function"><a href="function.stream-context-create.php" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Створює контекст потока</span></li>
    <li><span class="function"><a href="function.umask.php" class="function" rel="rdfs-seeAlso">umask()</a> - Changes the current umask</span></li>
    <li><span class="classname"><a href="class.splfileobject.php" class="classname">SplFileObject</a></span></li>
   </ul>
  </p>
 </div>


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