PHP Tutorial #25 File System Part 1 " Fopen, Fread, Fwrite and File_get_contents "

Image Source

Repository

https://github.com/php/php-src  

What Will I Learn?

  • You will learn how to handle the file using the Fopen method.
  • You will learn how to open the file with the Opening Mode for reading using the Fread.
  • You will learn how to write in a file using the Fwrite method.
  • You will learn how to get the content of a file using the file_get_contents method.

Requirements

System Requirements:
OS Support for PHP
  • Windows
  • macOS
  • Linux

Required Knowledge

  • HTML language
  • CSS language

Difficulty

  •  Intermediate 

Description

In this tutorial we will learn stuff in the file systems that PHP offers, if you are in the local you work on a management application for example in your pc, and you want to use a system for storing your data for that. We're going to think about databases, but with file management systems, PHP gives us another technique to quickly store our data without access to a DBMS, we can add, modify and delete them through very simple and very efficient methods.

Today we will take 4 methods that are more used to manipulate the files either .txt or another extension, we will learn the "Fopen, Fread, Fwrite and finally file_get_contents" with the necessary parameters.

Fopen Method

PHP to process a file and to say that this file can be processed is to use Fopen, Fopen will open the file for both known read and write operations.

It has 2 parameters:

The first parameter can be the name of the file itself or a path to the file itself.

The second is the opening mode :

The opening mode specifically defines file access, the operating system needs to know what you want to do with the file once it is open. It is from there that he can specify the accesses in writing, reading, etc.

Fread Method

The fread() method reads up to the value of parameter ' length ' in the file referenced by the first ' handle ' parameter, we can use the variable that we have used to open the file. It stops when one of the following conditions appears :

The value of parameter ' length ' have been read or we can say the size of the file.

The end of the file is reached.

A packet becomes available or the socket timeout time is passed.

It can be used effectively if we can read the complete file, but sometimes we need to read the file line by line for example or character by character and for that we will not use the fread but we will use other functions like fgets()...

Fwrite Method

To write in a file you must use one of the functions of php, and for that we will use the function fwrite that's a very important function and easy to handle, this function makes it possible to write the contents in the file passed as paremetre.

It has 3 parameters : 

The first parameter is the handle is a resource file system pointer that is usually created using the fopen() function.

The second parameter is the string, which is the content to write in the file.

The third parameter is the length, if the length is supplied the writing will stop after length bytes, or at the end of the string.

It will return the number of bytes written or FALSE if an error occurs.

File_Get_Contents Method

To read a file or to obtain the contents of a file we will use the function file_get_contents(), this function will return the data read in a string of character or FALSE if an error occurs.

This function can return FALSE, but it can also return a value equivalent to FALSE

It has 5 parameters : 

The first parameter is the file name.

The second is the include_path.

The third is the context.

The fourth parameter is the offset.

The fifth parameter is the maxlen.

The function will return an error if the file name is incorrect or maxlen is less than 0.

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/R4.php

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/R4I.php

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/R4T.php

Sort:  

Hi @alexendre-maxim,

Thank you for another tutorial into the video category.

You have done a good job in this tutorial by teaching 4 diffeent good concepts.

The delivery in your video tutorial is also improving with diction and structure.

Keep up the good work.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @rosatravels! Keep up the good work!

Thank you @rosatravels, I am happy with this response .

Hi @alexendre-maxim!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @alexendre-maxim!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!