PHP fopen() Function

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.

resource fopen ( string $filename , string $mode [, bool $use_include_path = FALSE [, resource $context ]] )

fopen() binds a named resource, specified by filename, to a stream.

Example -

ParameterDescription
filenameRequired. Specifies the file or URL to open
modeRequired. Specifies the type of access you require to the file/stream.
filenamePossible 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_pathOptional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well
    contextOptional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream

    Note

    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 \n as the line ending character, Windows based systems use \r\n as the line ending characters and Macintosh based systems use \r as the line ending character.

    If you use the wrong line ending characters when writing your files, you might find that other applications that open those files will "look funny". Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

    The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the 'b' in all other cases. If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.