PHP fopen() Function | How to write fopen Function open file or URL
PHP fopen() function is an inbuilt function that is used to open a file or an URL. basically, it is used to bind a resource to steam using a specific filename. The filename and mode to be checked are sent as parameters to the PHP fopen() function and it returns a file pointer resource, furthermore, if a match is found and a False on failure. Therefore, error output can be hidden by adding an ‘@’ in front of the function name.
When writing to a text file in PHP, be sure to use the correct line-ending character! Windows systems use \r\n, Unix systems use \n, and Macintosh systems use \r as the line ending character. furthermore, Windows typically offers a translation flag (‘t’) which will translate \n to \r\n when working with the file. therefore, you can also use ‘b’ to force binary mode. also, to use these flags, specify or prescribe either ‘b’ or ‘t’ as the last character of the mode parameter.
Syntax:
Parameter Used
There is the following fopen() function in PHP that accepts four parameters.
| Value | Description |
|---|---|
| $file | It’s a mandatory/compulsory parameter that specifies the file. |
| $mode | It’s a compulsory parameter that specifies the access type of the file or stream. It can have the following possible values:
|
| $include_path | It is an optional parameter that is set to 1 if you want to search for the file in the include_path (Ex. php.ini). |
| $context | It is an optional parameter that is used to set the behavior of the stream. |
Return Value: It returns a file pointer resource on success, or FALSE on error.
PHP fopen() Function Exclusion:
- When writing to a text file, then the correct line-ending character should be used based on the platform. For Instance, Windows systems use
\r\n, Unix systems use\n, and Macintosh systems use\ras the line ending character. - It is recommended or suggested to use the
'b'flag when opening files with fopen() function. - Therefore, an error of level E_WARNING is generated When the open fails.
- When safe mode is enabled, then PHP checks whether the directory in which the script is operating has the same UID (i.e. owner) as the script that’s being executed.
- If you are unsure whether the filename is a file or a directory, therefore, you may need to use the is_dir() function before calling the PHP fopen() function since fopen() function may also succeed when the filename is a directory.
php fopen() Function Example
There are the following programs of the fopen() function.
Example:1
|
1 2 3 4 5 6 |
<?php // Opening a file using PHP fopen() function // When function in read only mode $mynewfile = fopen("/home/documents/tutorialscan/resume.txt", "r") or die("This File does not exist!"); ?> |
Example:2
|
1 2 3 4 5 6 7 8 9 |
<?php // Opening a file using PHP fopen() function // when function in read/write mode $mynewfile = fopen("resume.txt", 'r+') or die("This File does not exist!"); $pointers = fgets($mynewfile); echo $pointers; fclose($mynewfile); ?> |
Example:3
|
1 2 3 4 5 6 7 8 |
<?php // Opening a file using PHP fopen() function // in read mode along with b flag $mynewfile = fopen("resume.txt", "rb"); $content = fread($mynewfile, filesize($mynewfile)); fclose($mynewfile); print $content; ?> |
Example:4
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php // Opening a file using PHP fopen() function // in read/write mode $mynewfile = fopen("resume.txt", "w+"); // writing to file fwrite($mynewfile, 'welcometotutorialscan'); // Setting the file pointer to 0th // position using rewind() function rewind($mynewfile); // writing to file on 0th position fwrite($mynewfile, 'helloportal'); rewind($mynewfile); // displaying the contents of the file echo fread($mynewfile, filesize("resume.txt")); fclose($mynewfile); ?> |
Conclusion:
In this article, you have learned PHP fopen() function with different file mode. I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com. I will try to resolve the problem.