alexendre-maxim avatar

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

alexendre-maxim

Published: 22 Mar 2019 › Updated: 22 Mar 2019PHP Tutorial #25 File System Part 1 " Fopen, Fread, Fwrite and File_get_contents "

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

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

Written by

Web Designer , Gaming Video Maker

Read more #utopian-io posts


Best Posts From alexendre-maxim

We have not curated any of alexendre-maxim's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From alexendre-maxim