This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
CSS Fundamentals
HTML Fundamentals
JavaScript Fundamentals
SQL Fundamentals
PHP Fundamentals
Module 1. Intro
Module 2. Data Types
Module 3. Operators
Module 4. Arrays, Superglobals
Module 5. PHP Forms
Module 6. PHP Advanced
Module 7. PHP OOP
Module 8. MySQL Database
Module 9. PHP XML
Module 10. PHP - AJAX
6.3 PHP File Handling
File handling is an important part of any web application. You often need to open and process a file for different tasks.
PHP Manipulating Files
PHP has several functions for creating, reading, uploading, and editing files.
Be careful when manipulating files!
When you are manipulating files you must be very careful.
You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.
PHP readfile() Function
The readfile() function reads a file and writes it to the output buffer.
Assume we have a text file called "webdictionary.txt", stored on the server, that looks like this:
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 PHP code to read the file and write it to the output buffer is as follows (the readfile() function returns the number of bytes read on success):
<?php
echo readfile("include/Module6Quiz.txt");
?>
The readfile() function is useful if all you want to do is open up a file and read its contents.
The next chapters will teach you more about file handling.
The value of i is: 20
Navigate this module
Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.
Animated PHP icons used in the buttons provided by ICONS8.COM .
Smartphone icons created by Freepik - Flaticon
Module 6 quiz
Example files created in this module:
The PHP Date() Function - Get a Date
The PHP Date() Function - Automatic Copyright Year
The PHP Date() Function - Get a Time
The PHP Date() Function - Get Your Time Zone
The PHP Date() Function - Create a Date With mktime()
The PHP Date() Function - Create a Date From a String With strtotime() part 1
The PHP Date() Function - Create a Date From a String With strtotime()part 2
The PHP Date() Function - Create Dates From Strings With strtotime()
The PHP Date() Function - Count The Number of Days To July 4 With strtotime()
PHP Include Files - PHP include Example part 1
PHP Include Files - PHP include Example part 2
PHP Include Files - PHP include Example part 3
PHP Include Files - PHP include Example part 4
PHP Include Files - PHP require Example
readfile()
fopen()
fgets()
feof()
fgetc()
PHP File Handling - PHP readfile() Function
PHP File Open/Read - PHP Open File - fopen()
PHP File Open/Read - fgets() Read Single Line
PHP File Open/Read - feof() Check End-Of-File
PHP File Open/Read - fgetc() Read Single Character
PHP File Upload
PHP Cookies - Create/Retrieve a Cookie
PHP Cookies - Modify a Cookie Value
PHP Cookies - Delete a Cookie
PHP Cookies - Check if Cookies are Enabled
PHP Sessions - Start a PHP Session
PHP Sessions - Get PHP Session Variable Values part 1
PHP Sessions - Get PHP Session Variable Values part 2
PHP Sessions - Modify a PHP Session Variable
PHP Sessions - Destroy a PHP Session
PHP Filters - The PHP Filter Extension
PHP Filters - Sanitize a String
PHP Filters - Validate an Integer
PHP Filters - filter_var() and Problem With 0
PHP Filters - Validate an IP Address
PHP Filters - Sanitize and Validate an Email Address
PHP Filters - Sanitize and Validate a URL
PHP Filters Advanced - Validate an Integer Within a Range
PHP Filters Advanced - Validate IPv6 Address
PHP Filters Advanced - Validate URL That Must Contain QueryString
PHP Filters Advanced - Remove Characters With ASCII Value > 127
PHP Callback Functions
PHP Callback Functions - Anonymous Function
PHP Callback Functions - Callbacks in User Defined Functions
PHP and JSON - json_encode() part 1
PHP and JSON - json_encode() part 2
PHP and JSON - json_decode() part 1
PHP and JSON - json_decode() part 2
PHP and JSON - Accessing the Decoded Values part 1
PHP and JSON - Accessing the Decoded Values part 2
PHP and JSON - Looping Through the Values part 1
PHP and JSON - Looping Through the Values part 2
PHP Exceptions - Throwing an Exception
PHP Exceptions - The try...catch Statement
PHP Exceptions - The try...catch...finally Statement part 1
PHP Exceptions - The try...catch...finally Statement part 2
PHP Exceptions - The Exception Object