PHP Tutorial #26 File System Part 2 and Filter Part 3

Image Source

Repository

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

What Will I Learn?

  • You will learn the FILTER_CALLBACK.
  • You will learn the ' filter_var_array ' method.
  • You will learn the ' filter_input ' and ' filter_input_array ' methods.
  • You will learn how to upload a file.
  • You will learn the ' fseek ' 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 continue the parts we learned in the previous tutorials, today we will see another type of filter like the " FILTER_VALIDATE " and " FILTER_SANITIZE " that is the " FILTER_CALLBACK " , and some functions " filter_var_array() ", " filter_input() " and " filter_input_array() ", that they form the third part of "filters".

And we will also study the second part about the files, as it is a tutorial with a medium level we will ignore the simple methods used to manage the files systems but they are very important, the two things we will see today for files are :  how to upload a file or an image for example and how to use the function " fseek() " to read and write in a specific position.

1- FILTER_CALLBACK

The " filter_callback " is a filter like the filters that we have studied in the previous tutorials, the word " call "  in English that is to say a call to person for example or something, and the word " back " or in back don't appear, so the callback means to call something back.

The callback will call a function in the page, the callback will be in the third parameter of the method ' filter_var() ', we have the parameter that is an array with the key "options" we must put the name of the function.

The term callback function is well known in the web on all programming languages, we will make an example to understand how to use it and why to use it.

2- Filter_var_array Method

If we have a registration form as the example that we always take, and we have to filter all the inputs, for that we will use the " filter_var() " function several times, that's correct but not the better, to improve our code and optimize the execution time, PHP offers an important function " filter_var_array() ", this function accepts two parameters or we can say two arrays, the first contains the values entered by the user or the values that we want to filter.

The second is the "args" or arguments, this array contains the filters for each element or for each value of the previous array.

3- Filter_input Method

If you noticed in the previous tutorial, I first recovered the value that the user entered, after I used the function " filter_var() " using the three parameters, today we will see how to use directly a function that will replace the recovery task with the values.

The " filter_input " function accepts 4 parameters :

The first parameter is the type, we have 5 types:

- INPUT_GET

- INPUT_POST

- INPUT_COOKIE

- INPUT_SERVER

- INPUT_ENV

The second parameter is the variable or the "name" of input.

The third parameter is the filter ' FILTER_VALIDATE ' , ' FILTER_SANITIZE ' or ' FILTER_CALLBACK '

The fourth parameter is the options or the array of options.

It returns the value of the variable on success, FALSE on failure  or NULL if the "variable" parameter is not set

4- Filter_input_array Method

As we used the " filter_var_array " function, we have the " filter_input_array " function, instead of using the ' filter_input ' function several times, we will use the " filter_input_array " function.

This function accepts two parameters

The first parameter is the type, as we saw we have 5 types

The second parameter is the array contains the filters or replace the parameters of " filter_input " function.

The second part of the file system will contain the file_upload and the fseek methode.

5- Upload file

Firstly we will take the " file_upload " , we look at almost all types of inputs, today we will see the type "File", how to upload a file or an image for example in the database, for that we need to use the " $_files " array.

There are several types of files, the " $_Files" array will return an array with :

- name : the filename with the extension.

- type : the uploaded file type

- tmp_name: the file tmp path

- error : an integer will return 0 or the number of errors , 0 if there are no error.

- size : the file size that you uploaded.

6- Fseek Method

We have seen the "fopen()" method that accepts two parameters, the file and the opening mode.

If we want to add a "string" in this file we will use one of the opening mode that allows the writing in the file, and we will use the function " fwrite() ", and it will add the string either in the beginning of file or in end.

If I want to insert a string in a defined place, for that we will use the function " fseek ".

This function accepts 3 parameters:

The first is the file processed by the method fopen.

The second is the offset or we can say, in which position I will start calculating or doing something.

The last parameter is the whence, this parameter contains 3 options:

- SEEK_SET

- SEEK_CUR

- SEEK_END

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/Files.html

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

https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/functionsFilter.html

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

Sort:  

Hi @alexendre-maxim,

You are making improvement in your video tutorial contributions.

I can see that you are well organized and you put effort in presenting the tutorial in a clear and structured way.

Keep moving forward in this direction.

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 @rosatravels, I will try to continue and improve more.

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

Congratulations @alexendre-maxim! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 3000 upvotes. Your next target is to reach 4000 upvotes.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

3 years on Steem - The distribution of commemorative badges has begun!
Happy Birthday! The Steem blockchain is running for 3 years.
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

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!

Coin Marketplace

STEEM 0.20
TRX 0.26
JST 0.039
BTC 100104.47
ETH 3619.58
USDT 1.00
SBD 3.10