How to Open a FIle in PHP using the fopen() Function (original) (raw)

Skip to content

Summary: in this tutorial, you will learn how to open a file in PHP using the fopen() function.

Introduction to the PHP fopen() function #

Before reading from or writing to a file, you need to open it. To open a file, you use the fopen() function as follows:

fopen ( string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">filename , string </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>mode , bool <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>s</mi><msub><mi>e</mi><mi>i</mi></msub><mi>n</mi><mi>c</mi><mi>l</mi><mi>u</mi><mi>d</mi><msub><mi>e</mi><mi>p</mi></msub><mi>a</mi><mi>t</mi><mi>h</mi><mo>=</mo><mi>f</mi><mi>a</mi><mi>l</mi><mi>s</mi><mi>e</mi><mo separator="true">,</mo><mi>r</mi><mi>e</mi><mi>s</mi><mi>o</mi><mi>u</mi><mi>r</mi><mi>c</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">use_include_path = false , resource </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9805em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal">s</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">n</span><span class="mord mathnormal">c</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">u</span><span class="mord mathnormal">d</span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">p</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">se</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">reso</span><span class="mord mathnormal">u</span><span class="mord mathnormal">rce</span></span></span></span>context = ? ) : resource Code language: PHP (php)

The fopen() has the following parameters:

The fopen() returns the file pointer resource if it opens the file successfully or false if it fails to open the file.

The following table shows the type of access of the $mode parameter:

Mode Reading Writing File Pointer File doesn’t exist, Create? If the File Exists
‘r’ Yes No At the beginning of the file No
‘r+’ Yes Yes At the beginning of the file No
‘w’ No Yes At the beginning of the file No
‘w+’ Yes Yes At the beginning of the file Yes
‘a’ No Yes At the end of the file Yes
‘a+’ Yes Yes At the end of the file Yes
‘x’ No Yes At the beginning of the file Yes Return false
‘x+’ Yes Yes At the beginning of the file Yes Return false
‘c’ No Yes At the beginning of the file Yes
‘c+’ Yes Yes At the beginning of the file Yes

When working with a binary file, you need to append the characer b to the $mode argument. For example, the 'rb' mode opens a binary file for reading.

After opening a file and completing working with it, you should always close the file. If you don’t close a file properly, it may cause many issues. For example, you may not be able to access the file again.

To close a file, you pass the file pointer to the fclose() function:

fclose ( resource $stream ) : boolCode language: PHP (php)

The fclose() function returns true on success or false on failure.

It’s a good practice to check if a file exists before opening it.

PHP open files example #

The following example uses the fopen() to open the readme.txt file for reading:

`<?php

$filename = 'readme.txt';

if (!file_exists($filename)) { die("The file $filename does not exist."); } f=fopen(f = fopen(f=fopen(filename, 'r'); if ($f) { // process the file // ... echo 'The file ' . $filename . ' is open'; fclose($f); } `Code language: HTML, XML (xml)

How it works.

Summary #

Did you find this tutorial useful?