Use the fopen() function to open a file or URL. If the function fails, it returns FALSE and an error message. Add '@' in front of the function name to hide error output.
fopen(file_path, mode, include_path, context)
file_path − The path to the file.
mode − The type of access you require to the file
incude_path − If you want to search for a file in include_path (in php.ini), Please set it to '1'.
context − The context of the file pointer.
The fopen() function returns FALSE and an error on failure. Add '@' in front of the function name to hide error output.
Suppose we have a file named "new.txt" with the following content.
The content of the file!
Now, let’s see an example -
<?php // read/ write mode $file_pointer = fopen("new.txt", 'r+') or die("File does not exist"); $res = fgets($file_pointer); echo $res; fclose($ile_pointer); ?>
The content of the file!
Let’s see an example of a “one.txt” file.
<?php // read/write mode $file_pointer = fopen("one.txt", "w+"); // writing to file fwrite($file_pointer, 'demo content'); echo fread($file_pointer, filesize("new.txt")); fclose($file_pointer); ?>
demo content
The above is the detailed content of fopen() function in PHP. For more information, please follow other related articles on the PHP Chinese website!