i
PHP Variables
PHP Data Types
PHP Echo & Print
PHP Strings
PHP Numbers
PHP Constants
PHP Operators
PHP if...else...elseif Statements
Switch Statement
PHP Loops
PHP Arrays
Superglobals
PHP Coding Standards
PHP Form Handling
PHP Form Validation
PHP URLs Validation
PHP Form Required Validation
Complete Form Example
PHP File Functions Open/Read
PHP File Create/Write
PHP File Upload
PHP Cookies Handling
PHP Session Handling
PHP filter_var() Function
PHP Validation Filters
PHP Sanitization Filters
Using Filters
Filters Advanced
JSON
PHP Date and Time
MySQL Database
MySQL Connect
MySQL Commands-Creating a Table
MySQL Commands-Inserting The data
MySQL Commands-Prepared Statement
MySQL Commands-Selecting The Data
MySQL Commands-Where and Order By
MySQL Commands-Deleting And Updating The Data
PHP-OOP Introduction
PHP-Classes/Objects
PHP-Constructor/Destructor
PHP-Access Modifiers
PHP-Inheritance
PHP-Inheritance and Protected Access Modifier
PHP-Overriding Inherited Methods
PHP-Final keyword
PHP-Abstract Classes
PHP-Constants
PHP-Traits
PHP-Static Methods and Properties
Introduction to Functions
Defining A function
Returning Values From A Function
Dynamic Function Calls
Variable Scope
Understanding Arguments Or Parameters
Testing For A Function Existence
Returning Multiple Values From A Function
Making practical Use By Building Code Libraries For Code Re-usability
Using Include() And Require()
While using a web application, you have to works with multiple files. As in a web application you need to open a file, read it, and process it also. So, file handling is required for multiple tasks in PHP. As you create a website, you will be using multiple file functions that we will explain in the next section in detail But let us know brief about file handling.
PHP has various functions for uploading, creating, reading, and editing any file. You must be very careful when you perform manipulation on files. If something went wrong, you may also lose the information in a file.
If you want to read a file and write it to output buffer, you can use readfile() function in php.
For example, if we have a file called “Weblanguage.txt” on a server having below information.
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = Extensible Markup Language
The below script will output the file and also return the bytes read from the file.
echo readfile(“weblanguage.txt”);
?>
readfile() is a useful function when you want to open a file and read all its contents
PHP File Functions
As we discussed in the last section, we have various file functions that we can perform in PHP. We can open and read a file, create a file, write or update a file, and upload a file to the server. So, let us discuss the file functions in detail.
1 File Open/Read
In php, to open a file you can use fopen() function and it gives you more options than readfile() function. You get to use the option to open a file in a specific mode as fopen() function accepts a parameter that specifies in which mode the file should be opened like read, write etc.
For example: fopen(“weblanguage.txt”, “r”); will open the text file in read mode.
You get below options for the modes in fopen() function.
r- It opens the file in only read format.
w – It opens the file for only write mode and it erases all the contents for the file and creates a new file if the file is not present.
a – It allows you to append a file and preserves the data of existing file.
x – It creates a new file for write mode and if the file is already present if returns FALSE and an error.
r+ - It opens a file with the read and write option.
w+ - It opens a file for read and write but erases all the contents for the existing file.
a+ - It opens a file for read and write but the existing data in a file is preserved.
x+ - It creates a new file for read and write and returns FALSE if the file is already present.
There are some other helpful functions used with an opened file as below:
fread() function is used to read the contents from an open file and it accepts two parameters. First parameter specifies the name of the file and second specifies the maximum bytes to read from that file. For example: fread($filename, filesize(“weblanguage.txt”));
fclose() function is used to close a file that is already opened. It is always good to close the files after your work is finished. It accepts one parameter that specifies the name of the file you need to close. For example: fclose($filename);
fgets() function is used to read a single line from the opened file. For example: fgets($filename); gets one line from the specific file.
feof() function checks if you have reached end of the file and is helpful to look when you are not aware of the file length. For example, you can look like: while(! feof($filename) { //code that needs to be executed}
fgetc() function is used to read a single character from the opened file. For example, fgetc($filename); It is helpful when you want to read a file character by character.
Don't miss out!