Basic File Handling in PHP – onlinecode

Basic File Handling in PHP – onlinecode

In this post we will give you information about Basic File Handling in PHP – onlinecode. Hear we will give you detail about Basic File Handling in PHP – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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.

Modes Description
r Read only. Pointers at the beginning of the file
r+ Read and Write both. Pointers at the beginning of the file
w Write 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
a Append. 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
x Write 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 :

  1. $my_file='myfile.txt';
  2. $handle=fopen($my_file,'w')ordie('Cannot open file: '.$my_file);
$my_file = 'myfile.txt';$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);

Read a File :

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

Have a look at given example :

  1. $my_file='myfile.txt';
  2. $handle=fopen($my_file,'r');
  3. $data=fread($handle,filesize($my_file));
$my_file = 'myfile.txt';$handle = fopen($my_file, 'r');$data = fread($handle,filesize($my_file));

Write to a File :

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

Have a look at given example :

  1. $my_file='myfile.txt';
  2. $handle=fopen($my_file,'w')ordie('Cannot open file: '.$my_file);
  3. $data='This is the data which you have to write in your file';
  4. fwrite($handle,$data);
$my_file = 'myfile.txt';$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);$data = 'This is the data which you have to write in your file';fwrite($handle, $data);

Close a File :

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

Have a look at given example :

  1. $my_file='myfile.txt';
  2. $handle=fopen($my_file,'w')ordie('Cannot open file: '.$my_file);
  3. //write some data here
  4. fclose($handle);
$my_file = 'myfile.txt';$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);//write some data herefclose($handle);

Delete a File :

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

Have a look at given example :

  1. $my_file='myfile.txt';
  2. unlink($my_file);
$my_file = 'myfile.txt';unlink($my_file);
See also  Laravel - How to count number of files in folder and remove all files in folder 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 :

  1. $file=fopen("myfile.txt","r")orexit("Unable to open the file!");
  2. while(!feof($file))
  3. {
  4. echofgets($file)."<br>";
  5. }
  6. fclose($file);
$file = fopen("myfile.txt", "r") or exit("Unable to open the file!");while(!feof($file)){echo fgets($file). "<br>";}fclose($file);

Label :

PHP

Hope this code and post will helped you for implement Basic File Handling in PHP – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

57  +    =  63

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US