Sharecodepoint popular queries

Sunday, 15 March 2020

File Handling in PHP

In any web application, file handling plays a very important role. For different processes, we need to open and process the file. For creating, reading, uploading files, there are many functions in PHP. Any kind of manipulation in the file can be done in a very careful manner. Sometimes files could be deleted or accidentally code gets deleted in some working file, the hard drive could be filled with garbage data, the wrong file chosen for working, etc. likewise many critical situations can occur while handling the file. Therefore file handling is always done carefully.

In PHP there is a read file() function defined for reading a file.

1. fopen() function is used to open a file. The fopen() function defined with two parameters.

<?php
$file = fopen("file.txt",'w');
?>

The first parameter in fopen() function is the name of the specified file and the second parameter is the mode for the particular file either read (r) or write (w).

Write mode modify the content of the existing file or it can be used to create the file for the same name if the file does not exist.
  • The (a) mode is used to write the content at the last of the page. The existing file is not deleted. The content can be added from last and the new file is created if the file does not exist.
  • The (x) mode used to write the file but if the file does not exist then it returns false as an error.
  • The (r+) command is used to read/write mode.
  • The (w+) mode is used for write/ read mode and through this content can be modified or deleted. read() function is used to read from an open file.


2. fclose() function is used for closing an open file. For the good developer, it is always recommended that the file must close first after completing the work on the same file.

<?php
$file = fopen("file.txt", 'r');
fclose($file);
?>

 If we want to read a single line from a file then we use fgets() function.

After calling fget() function, the pointer points to the next line and the process must go on.

The feof() function plays the role when the end of file is reached. It plays an important role in looping.

The fgetc() function is used to read the character from a file.

After calling the fgetc() function for one character then it points to the next character.

PHP is a very easy language and has very many useful functions. By using these functions we can be handling the PHP file easily and efficiently. Therefore File handling in PHP is very beneficial and necessary.

This article is contributed by Aayushi. If you like Sharecodepoint and would like to contribute, you can also write an article using sharecodepoint@gmail.com or mail your article to sharecodepoint@gmail. See your article appearing on the sharecodepoint main page and help another programmer.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

0 comments:

Post a comment

 

Version 23.2.3.4 - 2016 - 2020 - © Sharecodepoint | Made in India. | Copyright Content | Facebook | Twitter | Instagram | Youtube