PHP fopen() Function

## PHP Basic

PHP HOME PHP Intro PHP Install PHP Syntax PHP Variables PHP Echo / Print PHP Data Types PHP String Functions PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP While Loops PHP For Loops PHP Functions PHP Arrays PHP Sorting Arrays PHP Superglobals

## PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

## PHP Advanced

PHP Arrays Multi PHP Date PHP Include PHP File PHP File Upload PHP Cookies PHP Sessions PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter

## PHP Database

PHP MySQL Intro PHP MySQL Connect PHP CREATE DB/Table PHP INSERT INTO PHP SELECT PHP WHERE PHP ORDER BY PHP UPDATE PHP DELETE PHP ODBC

## PHP XML

XML Expat Parser XML DOM XML SimpleXML

## PHP and AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

## PHP Reference

PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones

## PHP Quiz

PHP Quiz PHP Certificate

# PHP fopen() Function

Complete PHP Filesystem Reference

## Definition and Usage

The fopen() function opens a file or URL.

If fopen() fails, it returns FALSE and an error on failure. You can hide the error output by adding an '@' in front of the function name.

## Syntax

fopen(filename,mode,include_path,context)

Parameter | Description
--- | ---
filename | Required. Specifies the file or URL to open

mode

Required. Specifies the type of access you require to the file/stream.

Possible values:

- "r" (Read only. Starts at the beginning of the file)
- "r+" (Read/Write. Starts at the beginning of the file)
- "w" (Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist)
- "w+" (Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist)
- "a" (Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist)
- "a+" (Read/Write. Preserves file content by writing to the end of the file)
- "x" (Write only. Creates a new file. Returns FALSE and an error if file already exists)
- "x+" (Read/Write. Creates a new file. Returns FALSE and an error if file already exists)

include_path | Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well
context | Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream

## Tips and Notes

Note: When writing to a text file, be sure to use the correct line-ending character! Unix systems use \n, Windows systems use \r\n, and Macintosh systems use \r as the line ending character. Windows offers a translation flag ('t') which will translate \n to \r\n when working with the file. You can also use 'b' to force binary mode. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

## Example

<?php
$file = fopen("test.txt","r");
$file = fopen("/home/test/test.txt","r");
$file = fopen("/home/test/test.gif","wb");
$file = fopen("http://www.example.com/","r");
$file = fopen("ftp://user:password@example.com/test.txt","w");
?>

Complete PHP Filesystem Reference

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE