0% found this document useful (0 votes)
21 views13 pages

Web Essentials

WEB ESSENTIALS

Uploaded by

Dr JANANI A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

Web Essentials

WEB ESSENTIALS

Uploaded by

Dr JANANI A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

WEB

ESSENTIALS
UNIT-4
SERVER SIDE PROCESSING
AND SCRIPTING-PHP
SEMINAR TOPIC

FILE HANDLING IN
PHP

PRESENTATION BY
S.KISHAN RAJ
B.TECH IT,2ND YEAR ,4TH SEM
311122205035
OVERVIEW
File handling in PHP refers to the various
functions and methods available in the
language that enable developers to read,
write, manipulate, and manage files and
directories on a server or local machine. PHP
provides several built-in functions like open (),
fwrite(), fread(), fclose(), and others to
manipulate files in different modes like read,
write, append, binary, etc. PHP also provides
functions to manage directories, such as
opendir() to open a directory, readdir() to read
the contents of a directory and closedir() to
close a directory. These functions enable
developers to create, move, rename, delete,
Introduction
File handling is a crucial aspect of programming that
involves creating, reading, writing, and deleting files. In
PHP, file handling plays a vital role in web development,
especially when dealing with tasks such as data
storage, retrieval, and manipulation. Understanding file
handling in PHP requires knowledge of the functions
and techniques available to effectively perform these
tasks.
PHP provides a rich set of built-in functions for file
handling, which can be used to perform a variety of
operations on files, including opening, reading, writing,
and closing files. With a good understanding of file
handling in PHP, developers can create powerful web
applications that can handle various file-related tasks
efficiently.
PHP fopen() Function
The fopen() function in PHP is used to open a file or URL and
returns a file pointer resource that can be used to read, write, or
manipulate the file.

The syntax of fopen() is as follows:


resource fopen ( string $filename , string $mode [, bool
$use_include_path = false [, resource $context ]] )
In this syntax:

$filename is the name of the file or URL to open


$mode is the mode in which to open the file. This can be one of
the following:
'r': read only
'w': write only (truncates the file to zero length or creates a new
file)
'x': exclusive write (creates a new file and
opens it for writing only if it doesn't already
exist)
'b': binary mode (used in conjunction with the
above modes to indicate that the file should be
opened in binary mode)
't': text mode (used in conjunction with the
above modes to indicate that the file should be
opened in text mode)
$use_include_path is a boolean parameter that
indicates whether to search for the file in the
include path (if set)
$context is an optional parameter that allows
you to specify a context for the file stream (e.g.
HTTP headers, SSL settings, etc.)
PHP fclose() Function
The PHP fclose() function is used to close an
open file pointer or handle in PHP. It is
typically used after a file has been opened
using fopen() or a related function.

Here is the syntax for the fclose() function:


bool fclose ( resource $handle )

The function takes a single parameter,


which is the file pointer or handles to be
closed. The function returns a boolean value
indicating whether the file was successfully
closed or not.
PHP fread() Function
The PHP fread() function is used to read a
specified number of bytes from a file. It is a file-
handling function in PHP that allows you to read
data from a file. Syntax:
fread($file_handle, $length)

Parameters:
$file_handle: Required. Specifies the file pointer or
file handle to read from. $length: Required.
Specifies the number of bytes to read from the
file.
Return Value:
Returns the read data as a string.
PHP fwrite() Function
The PHP fwrite() function is used to write data to a file.
It takes three parameters: a file pointer, a string to be
written, and an optional parameter that specifies the
maximum number of bytes to write.
Here's the syntax of the fwrite() function:
fwrite ( resource $handle , string $string [, int
$length ] ) : int|false

resource $handle: A file pointer, which is a reference to


the opened file. It can be obtained using functions such
as fopen(), fsockopen(), and tmpfile().
string $string: The string to be written to the file.
int $length: An optional parameter that specifies the
maximum number of bytes to be written. If this
parameter is not specified, all the data in the string will
be written to the file.
The fwrite() function returns the number of bytes
PHP Delete File - unlink()
In PHP, the unlink() function is used to delete a
file from the file system. It accepts a single
argument, which is the path to the file that
needs to be deleted.

Here is the syntax of the unlink() function:


bool unlink ( string $filename [, resource
$context ] )
Conclusion
File handling in PHP involves opening, reading,
writing, and manipulating files stored on a file
system.
The fopen() function is used to open a file for
reading, writing, or appending.
The fwrite() function is used to write to a file, while
the fread() function is used to read from a file.
The fgets() function can be used to read a single line
from a file.
The fclose() function should be used to close a file
after reading or writing to it.
THANK
YOU

You might also like