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

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center