You are on page 1of 22

BASIC FILE HANDLING

Prepared by:
ENGR. GIL POLIGRATES
OBJECTIVE

To be able to understand file handling like:


• opening and closing the file,
• creating a file,
• reading a file,
• writing a file,
• editing a file, and
• uploading a file.
WHAT IS FILE HANDLING?

PHP has different method or functions for creating,


reading, uploading, writing, closing, deleting files.

• File is nothing but a sequence of bytes stored in a


group.
• File are used to store your information.
OPENING & CLOSING A FILE

Before doing any activity with files you need to open the
file, also when you are going to open file then you can
set mode, in which you want to open. Mode will be like
read, write etc.

Modes Descriptions
r, r+, w, w+, a, a+, x, x+
OPENING & CLOSING A FILE

Modes Descriptions
r Read only. Pointers at the beginning of the file
r+ Read and Write both. Pointers at the beginning of the
file
w Write only. Opens and clears the contents of file; or
creates a new file if it doesn’t exist
w+ Read and Write both. Opens and clears the contents
of file or creates a new file if it doesn’t exist
OPENING & CLOSING A FILE

Modes Descriptions
a Append. Opens and writes to the end of the file or
creates a new file if it doesn’t exist
a+ Read and Append both. Preserves file content by
writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an
error if file already exists
x+ Read and Write both. Creates a new file. Returns
FALSE and an error if file already exists
OPEN A FILE

You can use fopen() function to open a file in PHP.


fopen() function takes two arguments, first contains the
name of the file and second contains the mode of file.

Example:
$my_file = 'myfile.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:
'.$my_file);
READ A FILE

You can use fread() function to read a file in PHP.

Example:
$my_file = 'myfile.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));
WRITE TO A FILE

You can use fwrite() function to write to a file in PHP.

Example:
$my_file = 'myfile.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:
'.$my_file);
$data = 'This is the data which you have to write in your
file';
fwrite($handle, $data);
CLOSE A FILE

You can use fclose() function to close a file in PHP.

Example:
$my_file = 'myfile.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:
'.$my_file);
//write some data here
fclose($handle);
DELETE A FILE

You can use unlink() function to delete a file in PHP.

Example:
$my_file = 'myfile.txt';
unlink($my_file);
READING A FILE LINE BY LINE
The fgets() function is used to read a single line from a
file and then file pointer is pointing to the next line in the
file. so by this way we can read file line by line.

Example:
$file = fopen("myfile.txt", "r") or exit("Unable to open
the file!");
while(!feof($file)){
echo fgets($file). "<br>";
}
fclose($file);
READING A FILE LINE BY LINE

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


been reached.

The fgetc() function is used to read a single character


from a file.
PHP – FILE UPLOADING

A PHP script can be used with a HTML form to allow


users to upload files to the server.

The process of uploading a file follows these steps:


• The user opens the page containing a HTML form
featuring a text files, a browse button and a submit
button.
• The user clicks the browse button and selects a file to
upload from the local PC.
PHP – FILE UPLOADING

• The full path to the selected file appears in the text


filed then the user clicks the submit button.
• The selected file is sent to the temporary directory on
the server.
• The PHP script that was specified as the form handler
in the form's action attribute checks that the file has
arrived and then copies the file into an intended
directory.
• The PHP script confirms the success to the user.
CREATING AN UPLOAD FORM
<html>
<body>

<form action="" method="POST">


<input type="file" name="image" />
<input type="submit"/>
</form>

</body>
</html>
CREATING AN UPLOAD SCRIPT
There is one global PHP variable called $_FILES. This
variable is an associate double dimension array and
keeps all the information related to uploaded file.

• $_FILES['file']['tmp_name'] − the uploaded file in the temporary


directory on the web server.
• $_FILES['file']['name'] − the actual name of the uploaded file.
• $_FILES['file']['size'] − the size in bytes of the uploaded file.
• $_FILES['file']['type'] − the MIME type of the uploaded file.
• $_FILES['file']['error'] − the error code associated with this file
upload.
CREATING AN UPLOAD SCRIPT
if(isset($_FILES['image'])){
// get the file information

// getting the file extension

// list of valid extensions for the uploaded file

// validate the file extension

// validate the size of the file

// display success if no error found


}
CREATING AN UPLOAD SCRIPT
// get the file information
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
CREATING AN UPLOAD SCRIPT
// getting the file extension
$file_ext=strtolower(end(explode('.',$_FILES['image']['name
'])));

// list of valid extensions for the uploaded file


$expensions= array("jpeg","jpg","png");

// validate the file extension


if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG
or PNG file.";
}
CREATING AN UPLOAD SCRIPT
// validate the size of the file
if($file_size > 2097152){
$errors[]='File size must be exactly 2 MB';
}

// display success if no error found


if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
END OF CHAPTER 5
THANK YOU!

You might also like