fopen Function - Open File in PHP
How to open a file in php?
function opens a file or URL.Usage:
fopen(filename,mode) To open a file using php
a) Call the function fopen with required arguments.
b) The first argument should be the file name. The filename should be opened under the current (same) directory.
c) The second argument should be the accessing mode of file.
- "r" (Read only)
- "r+" (Read/Write)
- "w" (Write only)
- "w+" (Read/Write)
- "a" (Write only)
- "a+" (Read/Write)
- "x" (Write only)
- "x+" (Read/Write)
The below code will explain the function more preciously,
$file = "test.txt"; // Here we define the file path
$open = fopen($file, "r"); // Here we open the file in read mode using the php method fopen()
In the above example, PHP automatically opens the file handled (i.e) test.txt, by the script.
fopen() fails, it returns FALSE and an error on failure.(i.e)Warning: fopen(tesgt.txt): failed to open stream.