Basic File Handling in PHP

Basic File Handling in PHP

Basic File Handling in PHP

PHP has different different method or functions for creating, reading, uploading, writing, closing, deleting files.

What is file handling in PHP

Before going to understand about file handling, you should first know what is file ?File is nothing but a sequence of bytes stored in a group.File are used to store your information and if you can make changes in your file whenever you want like:

  1. Opening & Closing File
  2. Creating File
  3. Reading File
  4. Writing File
  5. Editing File

Opening & Closing a File

Before doing any activity with files you need to open the file, also when you are going to open file then you can set mode, in which you want to open. Mode will be like read, write etc.

ModesDescription
rRead only. Pointers at the beginning of the file
r+Read and Write both. Pointers at the beginning of the file
wWrite only. Opens and clears the contents of file; or creates a new file if it doesn’t exist
w+Read and Write both. Opens and clears the contents of file or creates a new file if it doesn’t exist
aAppend. Opens and writes to the end of the file or creates a new file if it doesn’t exist
a+Read and Append both. Preserves file content by writing to the end of the file
xWrite only. Creates a new file. Returns FALSE and an error if file already exists
x+Read and Write both. Creates a new file. Returns FALSE and an error if file already exists
Open a File :

You can use fopen() function to open a file in PHP. fopen() function takes two arguments, first contains the name of the file and second contains the mode of file.

Have a look at given example :Read a File :

You can use fread() function to read a file in PHP.

Have a look at given example :Write to a File :

You can use fwrite() function to write to a file in PHP.

Have a look at given example :Close a File :

You can use fclose() function to close a file in PHP.

Have a look at given example :Delete a File :

You can use unlink() function to delete a file in PHP.

Have a look at given example :Reading a File Line by Line :

The fgets() function is used to read a single line from a file and then file pointer is pointing to the next line in the file. so by this way we can read file line by line.

Have a look at given example :

Phone: (+91) 8800417876
Noida, 201301
Betwinner
pendik escort
kartal escort