File handling is needed for any application. For some tasks to be done file needs to be processed. File handling in PHP is similar as file handling is done by using any programming language like C. PHP has many functions to work with normal files. Those functions are:
1) fopen() – PHP fopen() function is used to open a file. First parameter of fopen() contains name of the file which is to be opened and second parameter tells about mode in which file needs to be opened, e.g.,
Files can be opened in any of the following modes :
- “w” – Opens a file for write only. If file not exist then new file is created and if file already exists then contents of file is erased.
- “r” – File is opened for read only.
- “a” – File is opened for write only. File pointer points to end of file. Existing data in file is preserved.
- “w+” – Opens file for read and write. If file not exist then new file is created and if file already exists then contents of file is erased.
- “r+” – File is opened for read/write.
- “a+” – File is opened for write/read. File pointer points to end of file. Existing data in file is preserved. If file is not there then new file is created.
- “x” – New file is created for write only.
2) fread() –– After file is opened using fopen() the contents of data are read using fread(). It takes two arguments. One is file pointer and another is file size in bytes, e.g.,
3) fwrite() – New file can be created or text can be appended to an existing file using fwrite() function. Arguments for fwrite() function are file pointer and text that is to written to file. It can contain optional third argument where length of text to written is specified, e.g.,
4) fclose() – file is closed using fclose() function. Its argument is file which needs to be closed, e.g.,
This article is contributed by Swasthik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- File handling in Node.js
- How to reset selected file with input tag file type in Angular 9?
- How to get file input by selected file name without path using jQuery ?
- XML | Basics
- HTML | Basics
- Node.js | Basics
- HTML | SVG-Basics
- Basics of phpMyAdmin
- Exception Handling in PHP
- Exception handling in JSP
- Error handling in PHP
- HTML | Canvas Basics
- Laravel | Routing Basics
- Laravel | Controller Basics
- Laravel | Migration Basics
- Laravel | View Basics