- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP fopen(): Open a file
The PHP fopen() function is used when we need to open a file or a URL. For example:
<?php $myfile = "codescracker.txt"; if(fopen($myfile, "r")) echo "The file gets opened successfully, in 'r' mode."; else echo "Unable to open the file."; ?>
The output of the above PHP example is:
Since the file codescracker.txt is available in the current directory, it gets opened.
The fopen() function basically binds a named resource, specified by the name of a file, to a stream. For example, the above program can also be written as:
<?php $myfile = "codescracker.txt"; $fs = fopen($myfile, "r"); if($fs) echo "The file gets opened successfully, in 'r' mode."; else echo "Unable to open the file."; ?>
The $fs in the above example can be treated as a file handler, which is the variable used to handle the file opened by fopen(). Since the file is opened in reading mode, the only operation this file handler can perform on the file is the read operation.
PHP fopen() Function Syntax
The syntax of the fopen() function in PHP is:
fopen(fileName, fileOpeningMode, include_path, context)
The first two parameters are required, whereas the last two parameters are optional.
With the include_path parameter, you can include the path to look for the given file, where the context parameter is used to specify the context of the file handler.
Note: The fopen() function returns a file pointer or handler resource on success. Otherwise, it returns FALSE along with an error on failure.
PHP File Opening Modes: fopen() Modes
The following table lists and briefly describes all the file opening modes in PHP.
Mode | Description |
---|---|
r | opens the file for reading. |
r+ | opens the file for both reading and writing. |
w | opens the file for writing. |
w+ | opens the file for both writing and reading. |
a | opens the file for appending. |
a+ | opens the file for appending and reading. |
x | opens the file for writing. |
x+ | opens the file for both writing and reading. |
c | opens the file for writing. |
c+ | opens the file for both writing and reading. |
Note: The w, w+, a, and a+ modes create a new file if the specified file does not exist.
Note: The a and a+ modes preserve previous content and write new content at the end of the file.
Note: The x and x+ modes return FALSE if the file already exists. This mode is used to avoid overwriting.
Note: The c and c+ modes create a new file if the specified file is not available or does not exist.
PHP fopen() Function Example
Consider the following PHP code as an example demonstrating the "fopen()" function:
<?php $myfile = "codescracker.txt"; $fs = fopen($myfile, "r"); $myline = fgets($fs); while($myline!=null) { echo $myline; echo "<br>"; $myline = fgets($fs); } fclose($fs); ?>
The output of the above PHP example is:
The same program can also be written as:
<?php $fs = fopen("codescracker.txt", "r"); while(!feof($fs)) { $x = fgets($fs); echo $x . "<br>"; } fclose($fs); ?>
« Previous Tutorial Next Tutorial »