0% found this document useful (0 votes)
1 views35 pages

10 File Handling

The document provides an overview of file handling in PHP, detailing functions for reading, writing, and managing files. It covers various file modes, examples of functions like readline(), fopen(), fgets(), fwrite(), and methods for checking file existence and deleting files. Additionally, it explains how to rename files using PHP's rename() function.
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)
1 views35 pages

10 File Handling

The document provides an overview of file handling in PHP, detailing functions for reading, writing, and managing files. It covers various file modes, examples of functions like readline(), fopen(), fgets(), fwrite(), and methods for checking file existence and deleting files. Additionally, it explains how to rename files using PHP's rename() function.
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/ 35

PHP FILE HANDLING

FILE HANDLING

• Refers to a set of functions in PHP that enable


read/write operations on disk files with PHP code.
• A file object is classified as a stream.
READLINE() FUNCTION

• The readline() function in PHP accepts the user input from a


standard keyboard, and echo/print statements render the
output on the console.
• Designed for CLI (Command-Line Interface) execution, not for
web-based PHP scripts.
Sample Code:
<?php
$str = readline("Type something:");
echo $str;
?>
PHP - OPEN FILE
MODES OF OPENING A FILE

Modes Description
r Open a file for read only.
Open a file for write only. creates a new file
w
even if it exists.
a Open a file in append mode
x Creates a new file for write only.
MODES OF OPENING A FILE

Modes Description
r+ Open a file for read/write.
Open a file for read/write. creates a new file
w+
even if it exists.
a+ Open a file for read/write in append mode.
x+ Creates a new file for read/write.
Open the file for writing, if it doesn’t exist.
c However, if it exists, it isn’t truncated (as in w
mode).
FOPEN() EXAMPLE

<?php
$handle = fopen("hello.txt", "w");
$handle =
fopen("c:/xampp/htdocs/welcome.png", "rb");
$handle = fopen("http://localhost/hello.txt", "r");
?>
CLOSING A FILE

• It is always recommended to close the open stream


referenced by the handle

Example:
fclose($handle);
PHP - READ FILE
FGETS() FUNCTION

• The fgets() function can return a line from an open


file. This function stops returning on a new line at a
specified length or EOF, whichever comes first and
returns false on failure.
FGETS() EXAMPLE

<?php
$file = fopen("hello.txt", "r");
$str = fgets($file);
echo $str;
fclose($file);
?>
FGETS() EXAMPLE – READ FILE UNTIL END
OF FILE
<?php
$file = fopen("hello.txt", "r");
while(! feof($file)) {
echo fgets($file). "<br>";
}
fclose($file);
?>
FGETC() FUNCTION

• The fgetc() function returns a single character read


from the current position of the file handle. It returns
false when EOF is encountered.
FGETC() EXAMPLE

<?php
$file = fopen("hello.txt", "r");
$str = fgetc($file);
echo $str;
fclose($file);
?>
FGETS() EXAMPLE – READ FILE UNTIL END
OF FILE
<?php
$file = fopen("hello.txt", "r");
while(! feof($file)) {
$char = fgetc($file);
if ($char == "\n")
echo "<br>";
echo $char;
}
fclose($file);
?>
FREAD() FUNCTION

• The fread() function in PHP is a binary-safe function


for reading data from a file. While the fgets()
function reads only from a text file, the fread()
function can read a file in binary mode.
FREAD() EXAMPLE

<?php
$name = "hello.txt";
$file = fopen($name, "r");
$data = fread($file, filesize($name));
echo $data;
fclose($file);
?>
FSCANF() FUNCTION

• The fscanf() function in PHP reads the input from a


file stream and parses it according to the specified
format, thereby converts it into the variables of
respective types. Each call to the function reads one
line from the file.
FSCANF() EXAMPLE

<?php
$fp = fopen("employees.txt", "r");
while ($employee_info = fscanf($fp, "%s\t%s\t%s\t%d\n")) {
list ($name, $email, $post, $salary) = $employee_info;
echo "<b>Name</b>: $name <b>Email</b>:
$email <b>Salary</b>: Rs. $salary <br>";
}
fclose($fp);
?>
PHP - WRITE FILE
FPUTS() FUNCTION

• The fputs() function writes a string into the file


opened in a writable mode.
• The fputs() function returns the number of bytes
written, or false if the function is unsuccessful.
FPUTS() EXAMPLE

<?php
$fp = fopen("hello.txt", "w");
$bytes = fputs($fp, "Hello World\n");
echo "bytes written: $bytes";
fclose($fp);
?>
FPUTS() EXAMPLE - APPEND

<?php
$fp = fopen("hello.txt", “a");
$bytes = fputs($fp, "Hello World\n");
echo "bytes written: $bytes";
fclose($fp);
?>
FPUTS() EXAMPLE – COPY FILE TO
ANOTHER
<?php
$file = fopen("hello.txt", "r");
$newfile = fopen("new.txt", "w");
while(! feof($file)) {
$str = fgets($file);
fputs($newfile, $str);
}
fclose($file);
fclose($newfile);
?>
FWRITE() FUNCTION

• The frwrite() function is a counterpart of fread()


function. It performs binary-safe write operations.
• The fwrite() function returns the number of bytes
written, or false on failure along with E_WARNING.
FWRITE() EXAMPLE

<?php
$file = fopen("/PhpProject/sample.txt", "w");
echo fwrite($file, "Hello using fwrite() function");
fclose($file);
?>
PHP - FILE EXISTENCE
FILE_EXISTS() FUNCTION

• This function works with a file as well as a directory.


It checks whether the given file or directory exists or
not.
• The function returns true or false depending upon
the file exists or not.
FILE_EXISTS() EXAMPLE

<?php
$filename = 'hello.txt';
if (file_exists($filename)) {
$message = "The file $filename exists";
} else {
$message = "The file $filename does not exist";
}
echo $message;
?>
IS_FILE() FUNCTION

• The file_exists() function returns true for existing file


as well as directory. The is_file() function helps you
to determine if it’s a file.
• The is_file() function accepts a $filename and returns
true only if the $filename is a file and exists.
PHP – FILE EXISTENCE

is_readable() Function
• Sometimes, you may want to check before hand if
the file can be read from or not. The is_readable()
function can ascertain this fact.

is_writable() Function
• You can use the is_writable() function to can check if
a file exists and if it is possible to perform write
operation on the given file.
PHP – DELETE FILE

• PHP doesn’t have either a delete keyword or a


delete() function. Instead, it provides the unlink()
function, which when called, deletes a file from the
filesystem. It is similar to Unix/C unlink function.
• If the delete operation could not be completed, PHP
returns false and shows an E_WARNING message.
PHP – DELETE FILE

Example:
<?php
$file = "my_file.txt";
if (unlink($file)) {
echo "The file was deleted successfully.";
} else {
echo "The file could not be deleted.";
}
?>
RENAME A FILE IN PHP

• You can change the name of an existing file with the


help of respective command from the console of an
operating system.
• To rename a file programmatically, PHP’s built-in
library includes a rename() function.
RENAME A FILE IN PHP

Change the name of "hello.txt" to "test.txt"


Example:
<?php
rename("hello.txt", "test.txt");
?>

You might also like