Matthew M.

Matthew M.

Introduction to File I/O

Slide Duration:

Table of Contents

Section 1: Advanced PHP with MySQL
Course Introduction

13m 36s

Intro
0:00
Advanced PHP w/ MySQL
0:13
Course Introduction
0:14
Course Content
0:59
Web Application: Educator Store
1:45
Web Application: Educator Store
1:46
Object-Oriented Programming
6:09
Object-Oriented Programming Overview
6:10
MySQL
6:50
MySQL Overview
6:51
Example: Command Prompt & MySQL
8:34
What You Will Learn
9:34
What You Will Learn
9:35
Course Prerequisites
11:52
Course Prerequisites
11:53
Advanced Course Development Environment

18m 46s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Development Environment Setup
1:36
Firefox Web Browser, XAMPP, and PSPad Text Editor
1:37
Course Directory Structure
4:36
Course Directory Structure
4:37
phpDocumentor
7:23
phpDocumentor Overview
7:24
Parsing Source Code
7:44
docs Subdirectory
9:06
Asvanced PHP Course Documentation Example
10:21
Previous Educator PHP Students
14:14
Lib Directory Changes
14:38
Images Directory Changes
14:57
VERSION Constant
15:40
Comments Noting a Specific Version Number
17:04
Homework Challenge
17:30
Homework Challenge
17:31
Introduction to File I/O

35m 37s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
File I/O
1:34
File I/O Overview
1:35
Performing file I/O: 3 Steps
1:52
File Permissions
2:28
Read Permission & Write Permission
2:29
fopen()
4:47
fopen()
4:48
Two Required Parameters
5:01
Successful fopen(): File Handle and Resource
5:21
FALSE and E_WARNING Error
6:00
Example
6:39
File Paths
7:10
File Paths
7:11
File Access Modes
9:18
File Access Modes
9:19
Coding Example: fopen()
10:31
Using Relative and Absolute Path & Non-Existent File
10:32
File Pointers
15:55
File Pointers
15:56
Example File
16:11
Opening a File with fopen() Using 'r' as the Access Mode
16:22
fgets()
17:55
fgets() Overview
17:56
Coding Example: fgets() - Reading a Line at a Time
18:26
feof()
20:10
feof() Overview
20:09
Coding Example: feof()
20:50
fclose()
23:43
fclose() Overview
23:44
Coding Example: fclose()
25:02
Coding Example: Current Stock
26:10
Coding Example: Current Stock
26:11
trim ()
31:39
trim () Overview and Example
31:40
Homework Challenge
33:35
Homework Challenge
33:36
Web Application Development

36m 36s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
Version 1.0 Changelog
0:31
catalog.php
0:32
Function Library: fileLIB.php
5:00
Version 1.1 Changelog
27:12
createItemDataArray() and createDeptDataArray()
27:13
outputSimpleItemLink() and outputSimpleDeptLink()
32:06
HTTP & the POST Method

34m 36s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
HTTP Model
1:28
HTTP Model
1:29
Client-Server Model 2 Steps Process
2:00
HTTP Messages
2:23
HTTP Messages
2:24
HTTP Requests
3:09
HTTP Requests
3:10
Example: HTTP Request Header
3:17
Coding Example: HTTP Requests
4:29
HTTP Responses
7:15
HTTP Responses
7:16
Example: HTTP Response (Header & Body)
8:00
header()
11:47
header() Overview
11:48
Coding Example: header
14:02
Coding Example: header() Error
14:52
GET Method
18:55
GET Method
18:56
Coding Example: GET Method
20:13
POST Method
21:09
POST Method
21:10
Coding Example: Sample Form Using the POST Method
23:50
Accessing POST Data in PHP
26:38
Accessing POST Data in PHP
26:39
Coding Example: Accessing POST Data via $_POST
28:15
GET vs. POST
29:54
GET vs. POST
29:55
Example: Accessing POST Data via $_POST
31:20
Example: Accessing POST Data via $_POST
31:21
Homework Challenge
33:00
Homework Challenge
33:01
Web Application Development

14m 11s

Intro
0:00
Version 2.0 Changelog
0:09
Updating the Site's Current Form to Use the POST Method
0:10
Creating Admin Website
2:55
Dynamically Generated Department List
12:40
Writing to Files

17m 38s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Review of File I/O
0:40
Review of File I/O
0:41
fwrite()
1:38
fwrite()
1:39
fputs()
2:20
'r+'
2:31
Coding Example: fwrite()
2:59
fopen() Access Modes
8:41
'a' and 'a+'
8:42
'w' and 'w+'
11:34
Homework Challenge
14:47
Homework Challenge
14:48
Web Application Development

28m 14s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
Updated Open File Functions
0:19
openItemsDataFile ( ), openDeptsDataFile ( ), and openLastAddedFile ( )
0:20
insertItem()
6:25
insertItem() Overview
6:26
Functions: createItemDataString ( ) and updateLastItemAdded ( )
7:07
addItemToDepartment ()
16:03
addItemToDepartment () Overview
16:04
Functions: createDeptDataString ( ) and updateDepartment ( )
17:55
HTTP POST: File Uploads

22m 51s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
POST Data Encoding
1:13
POST Data Encoding
1:14
When Data is Included in the Body of the HTTP POST Request and is of the Content Type
1:31
When Uploading Files Using HTTP POST
3:17
Uploading Files
6:22
Uploading Files
6:23
If Encoding Type is Not Set to Multipart/Formdata
9:27
$_FILES Superglobal
10:59
$_FILES Superglobal
11:00
Structure of $_FILES
12:15
Structure of $_FILES: Name, Type, tmp_name, Error, and Size
12:16
Coding Example: $_FILES Superglobal
13:34
Moving an Uploaded File
15:39
Introduction to Moving an Uploaded File
15:40
move_uploaded_file ( ): Definition and Example
16:40
Homework Challenge
21:19
Homework Challenge
21:20
Web Application Development

15m 30s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Uploading Item Image File
1:22
Uploading Item Image File
1:23
Uploading Item Image File (cont.)
2:43
Altering insertItem ( )
2:44
Helpfer Functions: getFileExtension ( ) and moveUploadedFile ( )
4:30
isValidItem ( )
13:03
Introduction to Object-Oriented Programming

32m 44s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Object-Oriented Programming
0:56
Introduction to Object-Oriented Programming
0:57
Associative Arrays
5:27
Associative Arrays
5:28
Classes
7:27
Classes Overview
7:28
Defining Classes
8:24
Defining Classes
8:25
Declaring Class Properties
9:25
Coding Example: Classes
10:11
Objects
12:18
Objects Overview
12:19
Classes vs. Objects
12:49
Classes vs. Objects
12:50
Instantiating Classes
14:58
Instantiating Classes
14:59
Coding Example: Instantiate an Item Object
16:30
Object Properties
19:21
Access and Set an Object's Property
19:22
Coding Example: Set & Access the Properties of an Item Object
24:23
Homework Challenge
30:22
Homework Challenge
30:23
Web Application Development

17m 47s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 5.0 Overview
0:50
Version 5.0 Overview and Examples
0:51
Outputting Object Properties
12:38
Outputting Object Properties
12:39
Array Object Properties
14:18
Access and Set Array Value
14:19
Object Methods

39m 57s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Object Methods
0:52
Definition of Object Methods
0:53
Calling Object Methods
3:25
Calling Object Methods
3:26
Coding Example: Simple Object Methods
4:44
Object Methods and Return Values
7:02
$this Variable
10:06
$this Variable: Definition and Example
10:07
$this Variable (cont.)
15:51
$this Variable (cont.)
15:52
Getters & Setters
21:21
Getters & Setters Methods
21:22
Object Methods In Strings
25:46
Object Methods In Strings
25:47
Coding Example: Outputting Method Return Values in Strings
27:41
Using $this to Call Other Methods
28:49
Using $this to Call Other Methods
28:50
Homework Challenge
34:18
Homework Challenge: 1-3
34:19
Homework Challenge (cont.)
36:20
Homework Challenge: 4-6
36:21
Homework Challenge (cont.)
37:52
Homework Challenge: 7-10
37:53
Web Application Development

17m 30s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 6.0 Overview
0:40
Version 6.0 Changes & Examples
0:41
Item Methods
0:53
Item Class Definition: getImageFilename()
0:54
Coding Example: getImageFilename()
1:58
Department Methods
7:33
addItem(), removeItem(), and isItemInDept() Method
7:34
addItemtToDepartment() & buildDeptObject() Functions
11:46
A Default Value Needed to Be Set for $items
16:26
Object Constructors

22m 20s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Object Constructors
0:32
Introduction to Object Constructors
0:33
Coding Example: Object Constructors
1:43
Defining Constructors
3:26
Defining Constructors
3:27
Coding Example: Constructors with No Arguments
4:24
Constructor Arguments
8:09
Constructor Arguments
8:10
Coding Example: Constructor Arguments
9:49
Important Notes
16:13
Important Notes
16:14
Homework Challenge
17:35
Homework Challenge: 1 - 4
17:36
Homework Challenge (cont.)
19:13
Homework Challenge: 5 - 9
19:14
Homework Challenge (cont.)
21:40
Homework Challenge: 10 and 11
21:41
Web Application Development

16m 19s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
Version 7.0 Overview
0:39
Version 7.0 Overview
0:40
Item Constructor
2:19
Item Constructor
2:20
Department Constructor
7:47
Department Constructor
7:48
Customer Constructor
12:23
Customer Constructor
12:24
More Magic Methods

53m 37s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
Magic Methods
1:12
Magic Methods
1:13
Destructors
2:45
Destructors Overview
2:46
Coding Example: Calling Destructors
4:30
Coding Example: Object Destructor
9:19
_to String ()
16:12
_to String () Overview
16:13
Coding Example: _to String () Magic Method
18:10
Access Modifiers
21:23
Introduction to Access Modifiers
21:24
Access Modifiers: Public
21:39
Access Modifiers: Private
22:18
Access Modifiers: Protected
22:41
Object Properties and Methods
23:06
Coding Example: Public Access Modifiers
26:48
Coding Example: Private Access Modifiers
28:30
_get()
31:37
_get() Overview
31:38
Coding Example: _get () Magic Method
33:30
_set ()
36:23
_set () & the Magic Method
36:24
Using Getters & Setters
44:37
Coding Example: Using Getters & Setters
44:38
Homework Challenge
50:33
Homework Challenge: 1 - 6
50:34
Homework Challenge (cont.)
51:41
Homework Challenge: 7 - 12
51:42
Web Application Development

31m 12s

Intro
0:00
Version 8.0 Review
0:12
Version 8.0 Review
0:13
private Object Properties
1:15
private Object Properties
1:16
Coding Example
2:54
_toString() Methods
6:51
_toString() Methods
6:52
Coding Example
8:09
DataFile Class
13:27
DataFile Class & I/O Operations
13:28
Using DataFile Class: Instantiate a DataFile, DataFile open () Method, and DataFile close () Method
18:09
Homework Challenge
29:35
Homework Challenge
29:36
Classes vs. Objects

37m 49s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Class vs. Object Review
0:58
Class vs. Object Review
0:59
Static Properties
2:42
Static Properties
2:43
Scope Resolution Operator (::)
5:26
Scope Resolution Operator (::) & Static Property
5:27
Coding Example: Accessing a Static Property Using the Scope Resolution Operator
7:57
Coding Example: Accessing a Static Property in a Class Definition
10:33
Coding Example: Using a Static Property as an Instance Counter
14:18
Static Methods
18:51
Static Methods Overview
18:52
Coding Example: Static Methods
21:39
Classes vs. Objects
26:28
Classes vs. Objects: Diagrams
26:29
Class Constants
30:24
Class Constants Definition
30:25
Coding Example: Using Class Constants
31:55
Homework Challenge
34:01
Homework Challenge: 1 - 5
34:02
Homework Challenge (cont.)
35:25
Homework Challenge: 6 - 10
35:26
Web Application Development

21m 7s

Intro
0:00
Version 9.0 Overview
0:12
Version 9.0 Changes & Examples
0:13
Output Class
1:08
Output Class & Static Methods
1:09
outputHtmlSelector ( ) Method
2:43
outputItemSelector ( ) Method
4:53
outputDeptSelector ( ) Method
6:42
buildObject() Methods
11:54
buildObject() Methods & Examples
11:55
Exceptions

22m 43s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
try Blocks
0:44
try Blocks Definition
0:45
catch Blocks
2:03
catch Blocks: Definition and Syntax
2:04
Coding Example: try/catch Blocks
3:48
When an Exception Object is Caught & Error Handling
5:59
Exception Class
7:02
Exception Class
7:03
Coding Example: Using try/catch to Catch an Exception Thrown from a Method
8:04
Re-Throwing Exceptions
16:39
Re-Throwing Exceptions
16:40
Coding Example: Re-throwing an Exception
17:21
Homework Challenge
20:40
Homework Challenge: 1 - 5
20:41
Homework Challenge (cont.)
22:17
Homework Challenge: 6
22:18
Web Application Development

22m

Intro
0:00
Version 10.0 Overview
0:11
Updating addItem.php, addItemToDept.php, and error.php
0:12
Updating DataFile Class: open ( ) Method & try/catch blocks
7:45
Version 10.1 Overview
13:37
Version 10.1 Changes & Examples
13:38
Updating DataFile Class: close ( ) Method & logWarning ( )
20:17
Cookies

39m 47s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
What is a Cookie?
1:15
Definition of Cookie
1:16
HTTP - A Stateless Protocol
2:17
HTTP - A Stateless Protocol
2:18
Purpose of Cookies
4:10
Set - Cookie Header
6:21
Introduction to Set - Cookie Header
6:22
Coding Example: Set - Cookie Header
7:38
Cookie Header
9:45
Introduction to Cookie Header
9:46
Example: Cookie Header
10:27
setcookie()
12:33
Introduction to setcookie()
12:34
Example: Using setcookie
13:54
Cookies Attributes
15:48
Cookies Attributes Overview
15:49
Common Cookies Attributes: Domain
16:24
Common Cookies Attributes: Path
17:04
Common Cookies Attributes: Expires
19:34
Coding Example: Setting Cookie Attributes
20:58
$_COOKIE Superglobal
27:03
$_COOKIE Superglobal
27:04
Coding Example: Accessing Cookies from PHP Using $_COOKIE
29:45
Security Implications
34:44
Security Implications
34:45
Homework Challenge
36:10
Homework Challenge: 1 - 4
36:11
Homework Challenge (cont.)
37:47
Homework Challenge: 5 - 7
37:48
Web Application Development

39m 11s

Intro
0:00
Lesson Overview
0:17
Lesson Overview
0:18
instanceof Operator
1:34
instanceof Operator
1:35
Objects with Object Properties
2:53
Objects with Object Properties
2:54
Version 11.0 Overview
6:41
Version 11.0 Changes: Cart and CartItem Classes
6:42
Version 11.0 Coding Example
10:40
Version 11.1 Overview
31:03
Version 11.1 Changes & Examples
31:04
Introduction to Sessions

46m 8s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
What is a Session?
0:53
What is a Session?
0:54
Client-Side Sessions
3:51
Client-Side Sessions
3:52
Server-Side Sessions
5:04
Server-Side Sessions
5:05
Session IDs
6:26
Session IDs
6:27
The SID is Used by the Server to:
7:08
A Client Passes an SID to the Server with Each HTTP Request Via:
8:15
Sessions in PHP
12:19
Sessions in PHP
12:20
session_start()
14:40
session_start() Overview
14:41
Coding Example: Using session_start () to Continue a Session
18:03
$_SESSION Superglobal
22:06
$_SESSION Superglobal Overview
22:07
Coding Example: Setting a Session Variable via $_SESSION
25:22
Accessing Session Data
28:08
Accessing Session Data
28:09
Deleting Session Data
31:43
Deleting Session Data
31:44
Coding Example: Deleting Session Data
32:16
Configuring PHP Sessions
36:14
session.cookie_lifetime, session.cookie_domain, and session.cookie_path
36:15
Coding Example: Configuring PHP Sessions
37:28
Configuring PHP Sessions (cont.)
40:56
session.use_cookies, session.use_trans_sid, and session.use_only_cookies
40:57
Coding Example: Configuring PHP Sessions
42:25
Homework Challenge
42:55
Homework Challenge: 1 - 5
42:56
Homework Challenge (cont.)
44:10
Homework Challenge: 6 - 10
44:11
Homework Challenge (cont.)
45:27
Homework Challenge: 11 - 13
45:28
Web Application Development

11m

Intro
0:00
Version 12.0 Overview
0:12
Version 12.0 Changes Part I: Updating 'viewCart.php' & 'checkout.php'
0:13
Version 12.0 Changes Part II
5:03
Destroying Sessions

29m 59s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Destroying Sessions
1:02
Destroying Sessions
1:03
session_destroy()
2:10
session_destroy() Overview
2:11
Coding Example: Setting a Session Variable and Destroying a Session
3:18
Deleting Session Cookies
8:38
Deleting Session Cookies
8:39
Coding example: Deleting Session Cookies
9:17
Review of Steps
21:07
Review of Steps
21:08
Garbage Collection
21:50
Garbage Collection Overview
21:51
Coding Example: Garbage Collection
24:30
Homework Challenge
26:28
Homework Challenge: 1 - 4
26:29
Homework Challenge (cont.)
28:16
Homework Challenge: 5 - 9
28:17
Web Application Development

19m 14s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
_autoload()
0:46
Introduction to _autoload ()
0:47
Version 13.0 Overview
4:06
_autoload () and SessionManager Class
4:07
Updating 'viewCart.php' & 'checkout.php'
11:16
Introduction to Databases

21m 24s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
Flat Files
1:09
Flat Files: Definition and Example
1:10
Problems Associated with Using Flat Files as a Database
2:15
Relational Databases
3:29
Relational Databases
3:30
Relational Database Management System (RDBMS)
3:50
Tables
7:43
Tables
7:44
Columns
9:24
Columns
9:25
What is SQL?
10:45
Introduction to Structured Query Language
10:46
Standard Data Types of SQL
12:23
Primary Keys
13:19
Primary Keys
13:20
Primary Key Examples
16:36
Primary Key Examples
16:37
Introduction to MySQL

28m 11s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
What is MySQL?
1:33
MySQL
1:34
MySQL Monitor
5:16
MySQL Monitor Overview
5:17
XAMPP & MySQL
5:58
In the MySQL Command: hostname, username and -p
7:05
Connecting to a MySQl Server
9:52
Connecting to a MySQl Server
9:53
Using MySQl Monitor
13:48
Using MySQl Monitor
13:49
GRANT Command
15:10
GRANT SQL Command
15:11
Privileges & Objects
18:18
Username, Hostname, and Password
19:37
Specifying Objects
20:05
Creating a PHP User Account
21:41
Creating a PHP User Account Overview
21:42
Meaning of all & *.*
23:35
Example: Creating a PHP User Account
23:48
Required Homework
26:27
Required Homework: 1 - 5
26:28
Required Homework (cont.)
27:28
Required Homework: 6
27:29
Creating Databases & Tables

23m 1s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
Creating a Database
1:40
Introduction to Creating a Database
1:41
Example: Creating a Database
3:33
CREATE TABLE Command
6:43
CREATE TABLE Command
6:44
Data Types
7:39
Numeric Data Types: INT, INT UNSIGNED, and DECIMAL
7:40
String Data Types: CHAR (M), VARCHAR (M), and TEXT
9:23
Colum Attributes
11:00
PRIMARY KEY Attribute
11:01
AUTO INCREMENT Attribute
11:35
Items Table
13:07
Items Table
13:08
Useful Commands
17:04
SHOW TABLES Command & DESCRIBE Utility Statement
17:05
Example: Creating an Items Table
17:58
Required Homework
20:51
Required Homework: 1 - 6
20:52
Required Homework (cont.)
21:55
Required Homework: 7 - 9
21:56
SQL Command: INSERT

27m 11s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
INSERT Command
1:20
SQL INSERT Command
1:21
Specifying Values
3:16
Columns with Numeric Data Types
3:17
Columns with String Data Types
3:34
Columns with AUTO INCREMENT
4:07
Inserting Items
5:21
Inserting Items
5:22
Example: Inserting Items
7:03
SQL Text Files
14:04
SQL Monitor Client
14:05
MySQL Monitor & the < Operator
15:15
Example
17:35
Required Homework
22:32
Required Homework: 1 - 6
22:33
Required Homework (cont.)
24:19
Required Homework: 7 - 10
24:20
SQL Command: SELECT

24m 57s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
SELECT Command
0:42
SELECT Command: Definition and Syntax
0:43
Results of a SELECT Query
1:53
To Specify the Retrieval of All Columns from a Table
3:06
Example: SELECT Command
4:18
WHERE Clause
7:11
WHERE Clause
7:12
A SELECT Query with WHERE Clause has the Following Syntax
8:10
A Where Condition can Contain both Logical and Comparison Operators
9:44
Literal Values
13:24
Literal Values
13:25
Example: Literal Values
14:18
ORDER BY Clause
15:31
ORDER BY Clause & SELECT Query
15:32
Ascending and Descending Order
17:30
An ORDER BY Clause can Specify Multiple Columns to Order the Rows by
18:01
Example: SELECT Query with WHERE and ORDER BY Clause
20:32
Homework Challenge
21:59
Homework Challenge
22:00
Homework Challenge (cont.)
23:54
Homework Challenge
23:55
Using PHP from MySQL

32m 43s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
PHP/MySQL Architecture
0:56
PHP/MySQL Architecture
0:57
MySQLi Extension
3:05
MySQLi Extension
3:06
Steps to Using MySQL from PHP
5:53
Four Steps to Using MySQL from PHP
5:54
Step # 3
7:18
Connecting to a Database
8:12
To Connect to a MySQL Database
8:13
To Test for Connection Errors
10:20
Running an SQL Statement
11:54
Running an SQL Statement
11:55
Example
13:30
Processing the Results
15:58
For non-SELECT Queries
15:59
MySQLi Object Property: affected_rows
16:19
For INSERT Statements on Tables with an AUTO_INCREMENT Column
18:07
Closing the Connection
19:45
Closing the Connection
19:46
Coding Example
21:24
Coding Example: Using MySQL from PHP
21:25
Homework Challenge
29:00
Homework Challenge: 1 - 4
29:01
Homework Challenge (cont.)
31:10
Homework Challenge: 5 - 9
31:11
Web Application Development

30m 30s

Intro
0:00
Lesson Overview
0:14
Lesson Overview
0:15
sprintf()
1:28
sprintf() Overview
1:29
Example
1:56
INSERT Statements with sprintf()
5:15
INSERT Statements with sprintf()
5:16
Version 14.0 Overview
10:00
Version 14.0 Changes & Examples Part 1
10:01
Version 14.0 Changes & Examples Part 2
21:58
SELECT QUERIES from PHP

24m 22s

Intro
0:00
Lesson Overview
0:14
Lesson Overview
0:15
SELECT Queries
1:21
SELECT Queries
1:22
MySQLi_Result Class
3:17
MySQLi_Result Class: num_rows
3:18
MySQLi_Result Class: fetch_assoc ( ) and fetch_object ( )
3:59
fetch_assoc()
5:17
fetch_assoc() Overview
5:18
Coding Example: Using fetch_assoc()
9:00
stdClass Objects
12:46
stdClass Objects
12:47
fetch_object()
14:19
fetch_object() Overview
14:20
Coding Example: Using fetch_object()
16:45
Freeing Result Set Memory
18:42
Freeing Result Set Memory
18:43
Coding Example: Using free ( )
20:01
Homework Challenge
22:07
Homework Challenge: 1 - 5
22:08
Web Application Development

17m 6s

Intro
0:00
Version 15.0 Overview
0:13
Version 15.0 Changes & Examples Part 1
0:14
Version 15.0 Changes & Examples Part 2
8:43
Linking Tables

24m 32s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
A Departments Table
1:08
Departments Table
1:09
How Could We Model This in a Table: Option 1
1:26
How Could We Model This in a Table: Option 2
3:12
Database Normalization
4:37
Database Normalization
4:38
Foreign Keys
8:47
Foreign Keys
8:48
Linking Table
11:26
Linking Table
11:27
Multiple Column Primary Keys
15:30
Multiple Column Primary Keys
15:31
Example
18:51
Required Homework
22:34
Required Homework: 1 - 3
22:35
Required Homework (cont.)
24:10
Required Homework: 4
24:11
Web Application Development

24m

Intro
0:00
Version 16.0 Overview
0:11
Version 16.0 Changes
0:12
Version 16.0 Coding Examples
4:12
Version 16.0 (cont.)
18:30
Version 16.0 Changes & Examples Part 2
18:31
SQL Command: DELETE

10m 33s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
DELETE Statement
0:30
DELETE Statement & Its WHERE Clause
0:31
Delete All Rows From a Table
2:57
Using DELETE from PHP
4:04
Using DELETE from PHP Overview
4:05
Coding Example: Using DELETE from PHP
6:37
Homework Challenge
8:53
Homework Challenge: 1 - 4
8:54
Web Application Development

16m 2s

Intro
0:00
Version 17.0 Overview
0:11
Version 17.0 Changes
0:12
Version 17.0 Coding Example
2:51
Transactions
12:50
Database Transaction
12:51
Steps in Using Transactions
14:00
SQL Command: UPDATE

32m 14s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
UPDATE Statement
0:54
UPDATE Statement & Its WHERE Clause
0:55
Example: UPDATE Statement
3:32
Using UPDATE from PHP
5:21
Using UPDATE from PHP Overview
5:22
Coding Example: Using UPDATE from PHP
6:53
htmlspecialchars()
9:04
htmlspecialchars()
9:05
Encoding Special Characters
9:23
Coding Example: htmlspecialchars()
13:41
addslashes()
20:40
addslashes()
20:41
Coding Example: addslashes()
22:52
Homework Challenge
27:55
Homework Challenge: 1 - 2
27:56
Homework Challenge (cont.)
30:14
Homework Challenge: 3 - 5
30:15
Homework Challenge (cont.)
30:41
Homework Challenge: 6 - 9
30:42
Web Application Development

12m 33s

Intro
0:00
Version 18.0 Overview
0:12
Version 18.0 Changes
0:13
Version 18.0 Coding Example
1:24
SQL: Joins

42m 16s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
DATETIME Data Type
1:52
DATETIME Data Type
1:53
Modeling Orders
5:12
Modeling Orders
5:13
Customers Table
10:46
Customers Table
10:47
Example
16:18
Example: Order Table in MySQL
16:19
Joins
22:26
Joins Overview
22:27
Inner Join
23:09
Example: Joins
24:11
Join Conditions
28:14
Join Conditions
28:15
Example
28:32
Join Queries
30:00
Example: Join Conditions in MySQL
30:36
Implicit Joins
34:42
Implicit Joins
34:43
Example: Implicit Joins
35:57
Aliases
37:28
Introduction to Aliases
37:29
Example: Aliases
38:55
Required Homework
40:32
Problem 1
40:33
Problem 2
41:01
Problem 3
41:17
Web Application Development

29m 34s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Inserting Orders
0:44
Inserting Orders
0:45
Version 19.0 Overview
3:45
Version 19.0 Changes & Example Part 1
3:46
Version 19.0 Changes & Example Part 2
13:18
Version 19.1 Overview
21:10
Version 19.1 Changes
21:11
Version 19.1 Coding Example
22:18
User Authentication

26m 13s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
User Authentication
1:31
User Authentication
1:32
Authentication with Sessions
2:34
Authentication with Sessions
2:35
Four Steps in Authentication via Sessions
2:50
Using Sessions to Restrict Access
3:58
Using Sessions to Restrict Access
3:59
Coding Example: Restricted Access Area
4:47
Authentication Methods
5:54
Authentication Methods Overview
5:55
Coding Example: Authentication Methods
7:31
Logging Out
9:57
Logging Out
9:58
Coding Example: Log Out
10:47
Users Table
13:50
Users Table
13:51
Example: Creating a Users Table
15:08
Password Hashing
17:30
Password Hashing
17:31
PHP and MySQL Built-in Hash Functions: sha1 ( ) and md5 ( )
18:43
Coding Example: Password Hashing
19:27
Required Homework
24:41
Required Homework: 1 - 4
24:42
Web Application Development

14m 36s

Intro
0:00
Version 20.0 Overview
0:13
Version 20.0 Changes & Examples Part 1
0:14
Version 20.0 Changes & Examples Part 2
5:05
Version 20.0 Changes & Examples Part 3
7:29
Version 20.0 (cont.)
8:31
Version 20.0 Changes & Examples Part 4
8:32
Loading...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Advanced PHP
Bookmark & Share Embed

Share this knowledge with your friends!

Copy & Paste this embed code into your website’s HTML

Please ensure that your website editor is in text mode when you paste the code.
(In Wordpress, the mode button is on the top right corner.)
  ×
  • - Allow users to view the embedded video in full-size.
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Lecture Comments (4)

0 answers

Post by Jatin Soni on January 4, 2014

This is just wonder since so long no one reply to these comment. I am afraid now to keep subscribe here.

0 answers

Post by Adil Alkarkhi on May 21, 2013

Dose fopen function open the file's name or the content of the file? thank you

1 answer

Last reply by: sorin dragon
Sat Mar 12, 2016 5:10 AM

Post by Rakesh Satapathy on November 3, 2012

Can we just not just use "fclose($filehandle);" instead of assigning it to a variable? i tested, but not sure it closes or not?

Introduction to File I/O

  • File I/O typically involves three steps:
    1. Open a connection to a file
    2. Perform read/write operations on the file
    3. Close the connection to the file
  • In order to read from or write to a file from PHP, the user account under which the PHP interpreter runs must have the appropriate read/write permissions on the file, depending on the desired operation.
  • fopen() is used to open a file. You must specify whether you wish to open the file for reading, writing, or both. This is done using the function's access mode parameter.
  • fopen() returns a file handle, which represents a connection to an opened file. The file handle is a special PHP data type known as a resource.
  • File paths should always be specified using forward-slashes (/) as the path separator because it maximizes the portability of your code.
  • Data in a file is accessed using a file pointer, which is a cursor used to keep track of the current character being accessed in a file.
  • When opening a file with fopen() using ‘r’ as the access mode, the file is opened for reading with the file pointer placed at the beginning of the file.
  • fgets() reads the contents of the current line of a file and advances the file pointer to the beginning of the next line.
  • feof() returns TRUE if a file pointer’s current position is at the end of the file (EOF).
  • fclose() is used to close the connection to an open file by passing it the file handle of the open file.
  • trim() is used to strip any whitespace from the beginning or end of a string.
  • Additional Resources:

Introduction to File I/O

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:10
    • Lesson Overview
  • File I/O 1:34
    • File I/O Overview
    • Performing file I/O: 3 Steps
  • File Permissions 2:28
    • Read Permission & Write Permission
  • fopen() 4:47
    • fopen()
    • Two Required Parameters
    • Successful fopen(): File Handle and Resource
    • FALSE and E_WARNING Error
    • Example
  • File Paths 7:10
    • File Paths
  • File Access Modes 9:18
    • File Access Modes
  • Coding Example: fopen() 10:31
    • Using Relative and Absolute Path & Non-Existent File
  • File Pointers 15:55
    • File Pointers
    • Example File
    • Opening a File with fopen() Using 'r' as the Access Mode
  • fgets() 17:55
    • fgets() Overview
    • Coding Example: fgets() - Reading a Line at a Time
  • feof() 20:10
    • feof() Overview
    • Coding Example: feof()
  • fclose() 23:43
    • fclose() Overview
    • Coding Example: fclose()
  • Coding Example: Current Stock 26:10
    • Coding Example: Current Stock
  • trim () 31:39
    • trim () Overview and Example
  • Homework Challenge 33:35
    • Homework Challenge

Transcription: Introduction to File I/O

Hello again, and welcome back to Educator.com's Advanced PHP with MySQL course.0000

In today's lesson, we are going to be introducing the concept of file I/O, or file input and output.0005

Specifically, we are going to be going over what file I/O is; and as part of that discussion,0013

we are going to be talking about what file permissions are, which is something you may be familiar with,0017

especially if you have used a Linux or UNIX platform.0023

We are also going to be going over the different methods used to read from files.0026

The first method we are going to talk about is called fopen, which is a built-in function of PHP.0032

And as part of that, we are going to provide a discussion of specifying file paths to that function,0037

and also the different access modes that you can specify to the function,0042

that describe whether you want to open a file to read or write, for example.0047

We are going to go over the concept of file pointers, which is very similar to an array cursor,0051

which allows you to read or write from a certain point in a document, based on where this particular pointer is.0057

And then, we are going to go over a couple of other methods that relate to working with files.0064

fgets is a way of reading a single line from a file; feof is a function that tests if your file pointer is at the end of a file.0070

And we will explain more about that, and then we are going to go over the fclose method,0082

which is the analog to fopen, which is a way to close a particular file.0085

And we are also going to talk about the trim function, which is useful when working with files.0090

A file I/O, or file input and output, refers to reading from and writing to files in a file system.0098

PHP fortunately provides a number of built-in functions for doing so.0107

And in order to perform file I/O, there are typically three steps involved.0112

The first step is: you open a connection, or you open a particular file.0118

Then, what you do is perform any read or write operations that you need to on that particular file.0122

And when you are done with those operations, you close the file.0128

And so, we are going to, as we go through this lesson, walk through each of these different steps.0132

One thing to note about opening a file is that, when you open a file, we will be learning that you need to specify0139

whether you intend to just read or write from that particular file.0146

Now, we are going to talk about file permissions.0153

And like I said, those of you that use UNIX or Linux systems are more familiar with this than Windows users.0154

But in order to read or write from or to a particular file, you need to have the appropriate permissions on that file.0162

For example, to read from a file, you need to have what is known as read permissions on the file.0169

To write to a file, you need to have write permissions on the file.0174

And what these are, are permissions that are set by the particular operating system, on the system that you are using.0178

So, for our local development environment, it is going to be Windows's permission system.0184

If you are developing, if you are using a web host, they typically use a Linux server.0188

It is going to be dealing with Linux's permission systems for read and write permissions.0195

One of the things to note is that not having the proper file permissions for a particular file is one of the most common errors0202

in trying to do file I/O with PHP--if you are trying to read from a file (and you don't have read permission),0209

or write from the file (if you don't have permission to write on the file).0215

Now typically, with an operating system, the way permissions work is: they are associated with a particular user account.0220

For example, in Windows, when you log onto your computer, that is your user account.0227

And you have certain read and write permissions for different directories and different files.0231

Typically, you always have (for example) read and write permissions for all of the directories and files within your user directory.0236

When using PHP, one thing to note that you may encounter as a problem is that PHP, when the PHP Interpreter runs on your server,0244

it typically runs as a PHP user account, or as a web user account.0257

So, you have to make sure that the account associated with the PHP Interpreter0261

(and this is something that you may be able to change, or maybe it's something that you will have to talk to your system admin about)--0265

that that user account has to have a proper permission set to read or write for a particular file.0270

If PHP is trying to open a file for reading, whatever user that PHP Interpreter process is running at0277

needs to have permission to read or write from that particular file.0284

The first step in working with a file in PHP is to open the file for reading or writing.0290

And you do that using what is known as the fopen function.0295

Again, it is a built-in function of PHP, and it takes two required parameters.0299

The first one is the name and the path to the file that you are going to be opening.0304

The second is what is known as the read and write mode with which to open the file.0309

So, as mentioned, when you open a file, you have to let PHP and the operating system know whether you intend to0314

just read from the file, write from the file, or do both.0318

If fopen on a particular file is successful, it returns what is known as a file handle.0323

And what a file handle is: it represents an open connection to that particular file.0329

In PHP, a file handle is one of the special PHP data types (the other one being null) that is known as a resource data type.0335

Basically, it is a data type that refers to an external resource (in this case, a handle to an open file).0346

Another example of a resource data type might be a connection to a MySQL database.0354

If fopen fails, it returns false, and then it generates an E_WARNING-level error.0360

So, that is something that we are going to talk a little bit about working with--how to handle that error when you use the fopen function.0367

And this is a key function, because a valid file handle is needed by all of the other PHP file I/O functions that we are going to be dealing with.0374

because when you call up a function (for example) to read a line from a file, you have to pass it a valid file handle.0383

And that valid file handle means that it's a handle to an opened PHP file.0389

So, this is really a key function.0397

If we go and look at php.net and the documentation for the fopen function, we can see,0400

it has these two required parameters: file name and mode (mode being the read/write mode that you use to open a particular file).0406

And if you look down here, you can see: with mode, there are a number of different options that you use to open a file.0418

You can open it to read, open it to write...we are going to go over that a little more in a second.0423

File paths: as we saw, the first parameter to the fopen function is a file name parameter.0433

And you have to provide a path to the file that you want to open.0438

That can be a relative path or an absolute path, depending on the situation.0442

A couple things to notice about how to specify the paths: in Windows, you may know that paths are typically separated by the backslash character.0450

And on a UNIX system, they are actually separated by a forward slash character.0459

So, when you are specifying the path to a file in the fopen function, on a Windows system,0463

it lets you use either forward slashes or back slashes to specify the path.0471

And in order to specify a backslash within a PHP string literal, you have to escape it; so you will notice that,0479

for each backslash, you actually have two backslash characters.0485

On a UNIX system, which is maybe what your web host is going to be using, file paths are denoted with the forward slash character.0490

One thing to note is that, for the maximum code portability, you should always use forward slashes, even when working on a Windows environment.0499

For example, if this was specified...let's say this was just a path right here...this path is a relative path because it uses forward slashes.0507

If we move the application that uses this particular path from a Windows machine to a UNIX machine, the relative path would still work,0517

because the forward slash character can be used for both Windows and UNIX.0527

Now, that said, if we had instead used the backslash character to specify the path (and here I am eliminating the escape character),0532

now if we move our application to a Linux system, it is not going to be able to find that file in a relative path, because of the / character.0548

File access modes are the second parameter to the fopen function, and are a 1- to 2-character string0560

that tells PHP whether you want to open a file for reading, writing, or both.0568

And there are a couple other different options that you can do, as well.0572

One thing to note is that, in order to open a file for reading or writing, you have to have the proper permissions for that, as we had talked about.0577

You can request PHP, for example, to open a file to read; but if you don't have read permission on that file, you are not going to be able to do that.0585

And if we go back and look at the documentation for fopen, you can see: in the function documentation--the function reference--0592

there are a number of different modes that you can specify.0600

You can specify the character r (which is what we are going to be using in today's lesson)--a lowercase r that says0602

you are just opening the file to read, and you want the file pointer (which we are going to get to) to be placed at the beginning of the file.0607

We can use the w that says we are going to open the file for write.0615

There are modes for appending to the end of the file, and creating a new file, and so forth.0618

We are going to talk more about those when we get to the lesson on writing to files.0624

Let's go take a look at some sample code that we have.0632

There is a file called fopen.php; and we are going to use the fopen function in a couple of different ways to show how it is used.0635

We are going to be opening a file that is in the data directory of the directory that fopen.php is contained in.0646

So, fopen.php is in one directory; then, within that directory, there is another subdirectory called data.0659

For example, if we look at our lecture 3 examples, we can see that we have an fopen file;0665

and then, within that, there is a subdirectory called data that contains a file called stocks.txt.0671

This is the file that we are going to be trying to open.0677

And stock.txt, if we actually look at it, is a little terse; and we are going to explain more about that when we get to a future example,0681

but it basically is our way of representing the current stock for different items in a particular store.0690

For right now, we are just going to work with opening the file.0698

The first way we are going to do that is by using a relative path.0700

So, we specify a string called relPath that specifies a relative path to stock.txt.0703

And as you can see, it is in the data subdirectory where this fopen.php file is located.0712

And then, here we call the fopen function; we pass it the path name, or the path to the file, which is a relative path in this case;0720

and we pass it the access mode r, which says we want to open this file for reading.0726

Then, as we mentioned, if fopen does not work, what it will do is return false, saying that there was an error opening the file.0732

We test; it says that if file handle is false, we are going to output an error message that says, "There was an error opening the file."0741

And if it successfully opens it, we are going to output a message that says, "The file was successfully opened for reading."0748

Down here, we have another example that does the same thing, except we are going to use an absolute path and the fopen function.0754

So, instead of providing a relative path, we are going to provide an absolute path.0763

In this case, we define our absolute path using the SERVER ['DOCUMENT_ROOT'] superglobal array value.0766

And then, we are adding this specific path to our stock.txt file.0777

And as you can see, the same function call is made, except we use this absPath variable, which has an absolute path.0783

And again, we are going to test if there was an error opening the file; and if so, we are going to output an error message.0791

And if not, we are going to say that the file was successfully opened for reading.0796

If we go and load this script in our browser (go to fopen.php), we can see a couple of things.0801

We can see fopen using a relative path; and what it does is says, "The file" and it shows the relative path to that file "was successfully opened for reading."0809

So, we were able to open that file correctly.0817

And then, down here, when we use the absolute path, our output is--0819

and this is the absolute path to stock.txt--it says that file was successfully opened for reading.0823

We can see here an example of using both an absolute and a relative path with fopen.0829

There is a third example in this file, in which we try to generate an error using fopen, to show how it handles an error.0835

For example, we are going to try to open a file called stock.csv, which is an extension used for comma-separated value files.0843

You might have heard of it; it doesn't matter if you have or haven't.0855

But what you will notice is that, in our data subdirectory, in which fopen.php is located, there is no stock.csv file.0859

So, when we go to open this file, we should get an error from PHP.0871

And so, we should say, "There is an error opening this path."0875

And if we go and look at the end of the script (let's refresh it, just to make sure), we can see: at the bottom here,0877

it says, "warning: fopen on stock.csv failed to open"--the file doesn't exist.0885

And then, we have our output message that says, "There was an error opening stock.csv."0894

And one thing that is typically used here: because we have generated our own error output message here,0899

we don't really typically want this PHP warning to be output; it is kind of ugly, and it doesn't provide a good user experience.0907

So, we are going to make use of the error suppression operator, which is the @ symbol.0916

We are going to prepend that to the fopen function that is going to generate this error, so that, when we reload our page,0924

the PHP warning is not going to be output; and we are just going to have it output our own error message.0932

And any time we are going to be using fopen in this course (and as you will pretty much see any time you see fopen used),0937

it will be prepended with that error suppression operator, so that, if there is an error opening that file,0942

it doesn't output the ugly error message to your user's output.0948

So now, something I want to talk about is a file pointer.0956

It is kind of like an array cursor, which we learned about in the introductory course.0960

What it is: it is a cursor that is used to keep track of the current character being accessed in the file.0964

For example, if we have a file down here that has these three lines (which is a number of different characters,0972

separated by hidden new line characters here), when we are reading to or writing from a file,0978

we read to or write from a file based on the location of the file pointer.0986

For example, when we open a file using fopen with the r access mode, it opens the file for reading,0990

and it places the file cursor here, at the beginning of the file.0996

So, when we go to read (for example) the first four characters from this file, it is going to read from the current file pointer.1000

It is going to read up to here--the first four characters.1009

Now, for example (and there are different functions for moving this cursor around) that the file pointer was actually down here, before this i character.1012

Now, when we read four characters, it is going to read these four characters here: i, s, space, and l.1021

So, the file pointer is what is used to read from a file.1028

And also, if we are going to write to a file, when you write to a file, it is going to write to wherever that current location of the file pointer is.1031

One thing to note is that, sometimes, you will hear file handles referred to as file pointers; and that is commonly used.1040

In this course, we are going to distinguish between a file handle and a file pointer to make it easier to understand,1047

so that there is less confusion, because often, they are both referred to as file pointers.1054

So, for this course, we are going to use file handle to refer to the handle, or the connection, to an open file,1060

and then a file pointer to refer to the cursor that you use to access data within a file via reads or writes.1067

The first function we are going to introduce for reading from a file is called fgets.1077

And what it does is: it reads the contents of the current line of a file.1082

So, wherever the file pointer is--whatever line it is on--it reads out the contents of that file.1086

And if that pointer is in the middle of the line, it just reads from wherever that pointer is until the end of the line.1091

The other thing that it does is: after it reads the contents of the line, it advances the file pointer to the beginning of the next line.1098

Let's take a look at the fgets function in action.1106

We have a file called fgets.php, and we have a similar code to what we had in the last example, fopen.php.1109

We define a relative path to this stock.txt file; we open the file for reading.1117

We are using the r access mode, which is going to place the file pointer at the beginning of the file, so it will be at the very beginning of the first line.1124

You can see, we have the error suppression operator here, which is going to suppress any errors1131

(for example, "PHP can't find this stock.txt file").1136

If there is an error opening the file, we output an error message; if not, we move forward.1139

And then, what we do is call the fgets method.1144

And the fgets method takes at least one required parameter, that is the handle to the file that you want to read the current line from.1147

When we call fgets and pass it fileHandle, because fileHandle hasn't been used by any other (for example) read or write functions,1156

and we open the file with this r access mode, when you call fgets for the first time on a file handle, it is going to read the first line of the file.1165

So, what we do is call fgets on fileHandle and store the contents of that first line in this variable line.1174

And then, we output "contents of the first line are" whatever is stored in that variable.1179

If we go and view this script in our browser, when we go to fgets, we can see the output.1185

It says "contents of the first line is 100011."1192

And if we go back and look at stock.txt, we can see that that is the contents of the first line in the file.1197

That is how you use fgets to read a line from a file.1204

Now, typically you don't just want to read the first line of a file; you want to read multiple lines in a file, or even all of the lines in the file.1210

And one thing that is commonly used with fgets is another function called feof.1216

And what that allows you to do is test to see if the file pointer is at the end of the file.1224

For example, if you want to read all of the different lines in a file, and then stop reading from that file when you reach the end of the file,1227

you use this feof to function to let you know to stop reading lines, because you are at the end of the file.1235

And just so you know, eof stands for "end of file"; and it is commonly used to refer to when you have reached the end of a particular file.1241

If we go and look at a new piece of code called feof.php, what we are going to do is loop over all of the lines in that stock.txt file and output them.1252

Here, we have defined the relative path to the file; we have opened it using fopen.1269

And then, what we have done is created a loop that says, "As long as feof on this file handle on this file that we have opened is not true,"1276

meaning that it is not the end of the file yet, "it is going to continue to go through this loop."1287

And what it does is: when it enters into this loop, the first time, it is going to evaluate this test condition.1292

feof is going to return false, because it is not the end of the file; so not false is true.1298

We are going to enter the while loop, and the first thing we are going to do is get the current line in the file.1303

This, the first time we go through this, is going to get the contents of the first line in the file.1309

Then, what we are doing is simply outputting the line number of the file with the contents of that line.1313

And in order to do that, we have defined a lineNum variable, which is sort of like a loop counter that we are going to use to keep track of the line number.1319

Then, after it has output that line, it is going to increment the line number.1327

It is going to go back to the test condition of the while loop, and it is going to say, "Are we at the end of the file yet?"1331

In this case, because our file (as you have seen) has three lines in it, it is going to say, "No, we are not at the end of the file."1336

So, we are going to run the loop again, and we are going to go ahead and read the line and output it.1341

In this case, it is going to happen several times, and it is going to output each line of the file.1346

So, if we go and look at the results of this...we go to feof.php...we can see that it outputs line1=1000:5.1352

It outputs line 2 and line 3; and also, you can see, it tried to output a line 4.1362

You can see that it actually entered this loop a fourth time, but when it tried to output line, there was nothing there.1369

Well, the reason that happened is because in stock.txt, there are four lines, because at the end of this third line,1376

there is actually a new line character, which makes it line 4.1384

So, when feof is testing this file to see if it's at the end of the file, after it reads this third line, it is going to come down here1387

to this next line, and it is going to see that it is not the end of the file, because there is still a new line there.1398

And it is going to read the contents of that new line.1402

In this case, the new line has nothing on it, which is why it outputs nothing.1405

That is one thing you will have to be careful of, either by not including a new line at the end of your data files,1410

or testing for that condition (which is something we are going to show in an example in a minute).1415

As mentioned at the beginning of the lesson, there is a 3-step process for file I/O in PHP, or pretty much in most programming languages.1426

You open the file; you perform any read or writes that you need to on it; and then you close the file.1434

In this slide, we are going to talk about fclose, which is the analog to fopen; it is used to close the connection to an open file.1439

The way you do that is: you pass it a handle to an open file as its parameter, and what happens is that fclose goes ahead and closes that file.1446

Now, some people don't always close a file after they are done using it, but it is a recommended practice that you always1455

close any files that you have opened in your code, and also do so as soon as you are done using a file.1462

And part of the reason for that is something known file locking.1468

Typically, on a computer, only one process or program can access a file at the same time.1473

So, if you have a file open--if you have opened it, and you are doing a bunch of processing on it, and stop processing;1479

and you go off and do a bunch of other stuff, but haven't closed it; then any other program on the computer1485

trying to access that file is not going to be able to.1489

So, that is one of the main reasons for always closing a file when you are done with it, and doing it as soon as possible--1492

so that it is free for any other process to use.1498

Let's take a look at another code example called fclose.php.1502

And what this does is completes this 3-step loop.1507

We open our file using fopen, the same file as before.1513

We loop over each file, outputting it (we read each line of that file, and then output that line's contents).1516

And then, here you can see, we use the fclose method.1522

We have just finished using the file for all the reads that we need to, so we want to close it right away.1525

fclose returns true or false, depending on whether it was able to successfully close the file or not.1531

Based on that return value, if it was successfully closed, we are going to say "the file relPath was successfully closed."1537

If not, we are going to say there was an error closing it.1544

If we go and look at fclose.php, and we view it, we can see it outputs different lines of the file.1548

You will see here, it still outputs line 4, because we have that new line in the file.1555

And it is going to output the message at the end: "The file ./data/stock.txt was successfully closed."1559

And so, that is an example of using the fclose method.1567

Now, we are going to take a look at a little more real-world example in a file called currentStock.php.1571

What it is going to do is read this stock.txt file, and it is going to output for the user the current stock of each item number in the store.1576

For example, in this file, the way stock.txt is set up is: the first part of each line is an item number of an item in this fictitious store, for example.1588

And then, a colon is entered; and then after the colon is the current stock of the item.1600

And we put each item and its current stock on a new line in our stock.txt file.1609

For example, this file states that there are five items with item ID 1001 currently in stock.1615

It is out of stock of item 1002, because the quantity here is 0; and it has 24, currently, of item 1003.1625

What we do in this currentStock.php script is: we open for reading this stock.txt file.1636

And what we do is: we are going to loop over each line of the file, using this feof text function.1646

At each line that we are going to go to, we are going to read that line, using the fgets method.1653

What that is going to do is return (for example, when we read the first line) 100011.1659

Then, what we want to do is separate that information out into something useful.1666

We want to separate it out into an item ID and the current stock.1668

So, we are going to use the explode function and specify that we are going to be using the colon as the delimiter.1672

And we are going to say, "Take this string that we read from the file and separate it into parts, based on where the colon is."1680

And it is going to generate an array, and the first part of that array, or the value with the 0 index of that array, is going to be the item ID.1689

The value with the next index, the 1 index, is going to be the current stock.1696

Here, we do a little test that says, "If the length of lineArr is not equal to 2, then continue processing the next line."1703

And this our way of correcting for when we reach this fourth line here.1711

Instead of outputting the stock for this line, that is for an item that doesn't exist, when it reads this line,1717

and it runs explode on it, there is no colon; it is not going to return an array that is of size 2.1728

So, we are going to just say, "Skip over processing it."1733

And that way, that empty line doesn't get output.1735

Now, if the array does equal 2 in size, then we are going to extract the item ID and current stock amount from the array.1741

And here, we are just creating some short variables: we are going to say, "Item ID is equal to the value of the array at the 0 index,1749

and the current stock is equal to the value of the array at the 1 index."1755

And then, we are just going to output a line that says "Item ID has so-and-so items in stock, based on what was in the file."1760

We are going to do that for each of the items in the file.1769

And then, what we are going to do is close the file using the fclose method.1772

And if there is an error, we are going to generate an error message that says "There was an error generating the code."1776

If we go and look at this file, currentStock.php, we can see that it read over the file, and it said:1786

"Item 1001--there are 5 in stock; item 1002--0 in stock; item 1003--there are 24 in stock."1794

And so, this is the output of this file.1804

And one thing you will notice is that it has put "in stock" on a separate line in this page.1805

And if we look back here, and we look at our string to output, we don't have a new line or a break in there.1812

And so, what is happening is: an extra new line is getting into there, being added to that.1822

And because we are enclosing this output in the pre tag, it outputs the text as is.1828

So, if there is a new line character, it outputs it as a new line, or makes it look like a break.1836

If we look at the source code here, we can see that, after 5, there is a new line (let me blow this up) in the code.1840

And you can see, "in stock" is on a separate line.1851

Similarly, when we process item 2 after 0, we can see that a new line is output.1855

The reason for that is: if we look at stock.txt, each of these lines has two parts.1860

It has the item ID, and then a colon; and then it has the current stock.1867

But also, there is a new line character here (which allows us to put the second item on the next line).1875

So, when explode runs on this line, it is going to return, as the second element, 5 and a new line character1882

(which is why that new line character is going to show up).1891

And so, now, we are going to talk about a way to get rid of that new line character.1894

PHP provides a built-in function called trim; and it is often used; and what it does is strips any white space from the beginning or end of a string.1901

And it is often used to strip new line characters from lines read from a file.1910

So, what we are going to do: if we go back to currentStock.php, here, when we get the current stock from this lineArr1915

that we have created using the explode method, we are going to run the trim function on that array value.1929

And what that is going to do is strip that new line character off of that current stock,1936

because when it reads this line (for example, the first line), it reads 5 and then a new line.1944

Well, now, when it gets that string here by lineArr index 1, and we run trim on it, it is going to get rid of that new line.1950

So, all that is going to be left in that string is 5.1959

So, if we save this, and we go and look at the file again, we are going to see that now that new line doesn't appear.1961

And the string appears as we had written it, with "in stock" being on the same line.1970

So, it says "item number 1001 has 5 in stock."1973

And if we look at our source code, we can see that none of those (let's blow it up) new lines after the current stock appear anymore.1977

And so, typically, any time we are going to be reading stuff from a file, we are going to be running the trim command on it, as well.1988

So, we are going to do that for itemID.1995

In this case, it is not going to matter; but in some cases it does, so just as a good practice, when we read from a file,1997

and we use (for example) this explode method, we are going to trim any data that we get.2004

If we just run it again, everything is going to look the same; but that is just good practice.2010

For the homework challenge for today's course, I want you to do something similar to what we did in today's lecture.2018

I want you to create a data file called users.txt, where each line is a comma-delimited string2023

(in our example in this lecture, we used a colon-delimited string)--2029

a comma-delimited string that represents the first name, last name, and email address of the user.2033

And so, for example, one user in this data file might be represented like this:2040

Tom, Stevens (this is his first name and his last name), [email protected] (it's his email address).2045

You won't see it here, but there will actually be a new line character.2053

You will go to a new line and add another first name, last name, and email address for a particular user.2055

So, go ahead and populate this file, users.txt, with several fictitious users, just for use in this example.2062

And then, what I want you to do is: from PHP script, open this users.txt file; read each line; and then, after reading each line,2070

output the name of each user and the email address of each user.2079

You are going to have to make use of that explode command that we use in this thing.2085

And also, I want you to make sure that you use all of the functions that we used in the course today.2089

Use the fopen function to open the file; fgets to read a particular line; feof to test when you have reached looping over the lines in a file;2094

fclose (which is important) so that you close a particular file; and then use the trim function, so that you don't have that error2105

that we saw when we read a line from our file in today's lecture, where it adds an extra new line to a string that it reads from a file.2112

And test that example out on this text file that you create, and then see the name and email address output for each different user.2121

That ends today's lesson; thank you for watching Educator.com--I look forward to seeing you next time.2132

Educator®

Please sign in to participate in this lecture discussion.

Resetting Your Password?
OR

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Membership Overview

  • Available 24/7. Unlimited Access to Our Entire Library.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lecture slides for taking notes.
  • Track your course viewing progress.
  • Accessible anytime, anywhere with our Android and iOS apps.