0% found this document useful (0 votes)
5 views

File Handling

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

File Handling

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

FILE HANDLING

• A file is simply a resource for storing information on a computer.


• Files are usually used to store information such as;
1. Configuration settings of a program
2. Simple data such as contact names against the phone numbers.
3. Images, Pictures, Photos, etc.
PHP File Handling

• 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 FILE FORMATS SUPPORT
PHP file functions support a wide range of file formats that include;
1. File.txt
2. File.log
3. File.custom_extension i.e. file.xyz
4. File.csv
5. File.gif, file.jpg etc
6. Files provide a permanent cost effective data storage solution for simple
data compared to databases that require other software and skills to
manage DBMS systems.
7. You want to store simple data such as server logs for later retrieval and
Analysis
You want to store program settings i.e. program.ini
FILE ATTRIBUTES

• File attributes are the properties of a file, for example its size, the last
time it was accessed, its owner, etc.
filesize()

• The function filesize() retrieves the size of the file in bytes.

• If the file doesn’t exist, the filesize() function will return false and emit
an E_WARNING.
file_exists()

• This functions is used to check first whether the file exists or not.
• It comes in handy when we want to know if a file exists or not before
• processing it.
• You can also use this function when creating a new file and you want
to
• ensure that the file does not already exist on the server
FILE HISTORY

• To determine when a file was last accessed, modified, or changed,


you can use
• the following functions respectively: fileatime(), filemtime(), and
filectime()
FILE PERMISSIONS
• Before working with a file you may want to check whether it is
readable or writeable to the process. For this you’ll use the functions
is_readable() and is_writeable()
FILE OR NOT?

• To make absolutely sure that you’re dealing with a file you can use the
is_file()
• function. is_dir() is the counterpart to check if it is a directory.
OPENING AND CLOSING FILES
A better method to open files is with the fopen() function. This function
gives you more options than the readfile() function.
open(): The PHP fopen() function is used to open a file. It requires two
arguments stating first the file name and then mode in which to operate.
• Syntax: fopen($file_name,$mode);
Here,
• “fopen” is the PHP open file function
• “$file_name” is the name of the file to be opened
• “$mode” is the mode in which the file should be opened Places the file pointer
at the end of the file. If files does not exist then it attempts to create a file
Files modes can be specified as one
of the six options in this table.
r
• Opens the file for reading only.
• Places the file pointer at the beginning of the file.
r+
• Opens the file for reading and writing.
• Places the file pointer at the beginning of the file
w
• Opens the file for writing only.
• Places the file pointer at the beginning of the file and truncates the file to
zero length. If files does not exist then it attempts to create a file
Files modes can be specified as one
of the six options in this table
w+
• Opens the file for reading and writing only.
• Places the file pointer at the beginning of the file and truncates the file to
zero length. If files does not exist then it attempts to create a file.
a
• Opens the file for writing only.
• Places the file pointer at the end of the file. If files does not exist then it
attempts to create a file.
a+
• Opens the file for reading and writing only.
READING A FILE

fread():
• Once a file is opened using fopen() function it can be read with a function
called fread().
• This function requires two arguments. These must be the file pointer and
the length of the file expressed in bytes.
• The files length can be found using the filesize() function which takes the
filename as its argument and returns the size of the file expressed in bytes.
• The steps required to read a file with PHP.
The fclose() function is used to close an open file.

fclose(): After making a changes to the opened file it is important to close it


with the fclose() function. The fclose() function requires a file pointer as its
argument and then returns true when the closure succeeds or false if it
fails.
• Syntax: fclose($handle);
• Here,
• “fclose” is the PHP function for closing an open file
• “$handle” is the file pointer resource.
The steps required to read a file
with PHP.
1.Open a file using fopen() function.
2. Get the file's length using filesize() function.
3. Read the file's content using fread() function.
4. Close the file with fclose() function
• PHP Overwriting
• PHP Append Text
• You can append data to a file by using the "a" mode. The "a" mode
appends text to the end of the file, while the "w" mode overrides
(and erases) the old content of the file.
PHP Read Single Line - fgets()

• The fgets() function is used to read a single line from a file.


• The fgets() function returns a line from an open
file.
• Note: After a call to the fgets() function, the file pointer has
moved to the next line.
PHP Check End-Of-File - feof()

• The feof() function checks if the "end-of-file" (EOF) has been


reached.
• The feof() function is useful for looping through data of
unknown length
PHP Read Single Character - fgetc()

• The fgetc() function is used to read a single character from a file.


• The fgetc() function returns a single character from an
open file.
• Note: This function is slow and should not be used on
large files. If you need to read one character at a time
from a large file, use fgets() to read data one line at a
time and then process the line one single character at a
time with fgetc().
•.

You might also like