php file handling|read|write|append operation with example

php file handling|read|write|append operation with example in 2021
PHP file handling|read|write|append operation with example in 2021

PHP file handling|read|write|append operation with example

File Handling: File handling is a process to store the data into a file permanently.

It allows to create the file, write into a file, read from a file, append data into the file, etc.

Python File Handling

PHP File Operation

1. File opening.

2. File Writing.

3. Data appending into a file.

4. File Reading.

5. File Closing

The function used in file handling

Function       Description

fopen(): It is a predefined function that is used to open a file on a defined path.

It has two parameter file paths and keywords for reading(r) or writing(w). 

fwrite(): It is also a predefined function that is used to write something data or information into the file. It has two parameter files and a message which is to be written.

fclose(): It is used to close the file operation.

fread(): It is used to read the content of the file. It has two parameters resource and file size.

filesize(): It is used to get the file's length.


File Mode in file handling

File Mode     Description

R: It opens an existing text file for reading purposes. If the file does not exist, fopen() returns NULL.

r+: It opens an existing text file for reading and writing purposes. If the file does not exist, fopen() returns NULL.

W: It opens a text file for writing purposes. If it does not exist, then a new file is created. If the file exists, its contents are overwritten.

w+: It opens a text file for writing and reading purposes. If it does not exist, then a new file is created. If the file exists, its contents are overwritten.

A: It is used to open a text file in writing appending mode. If the file does not exist, then a new file is created.

a+: It is used to open a text file in writing and reading appending mode. If the file does not exist, then a new file is created.


C++ Files Handling

File Writing: It is used to write the content of a string into a file.

fwrite(): function is used to perform this operation.


How To Writing File in php?

<?php

$path="C:/myfile.txt";

$f=fopen($path,"w");

fwrite($f,"Hello friends Welcome To Php Language");

fclose($f);

?>

*****OUTPUT*****

Hello friends Welcome To Php Language

File Appending

1. Actually there is a problem with mode w because it deletes the existing content of the file then writes the new content therefore to add new content with the previous file appending is used.

2. It is used to add content to the end of the file.

3. Actually it places the file pointer at the end of the file.

4. For doing this just replace the mode w with a in fopen( )function in previous example.

<?php

$path="C:/myfile.txt";

$f=fopen($path,"a");

fwrite($f,"<br> Hello friends Welcome To Icoderweb website");

fclose($f);

?>

*****OUTPUT*****

Hello friends Welcome To Php Language

File Reading: It is used to read the content of a file.

fread(): function is used to perform this operation.

<?php

$file="D:/myfile.txt";

$f=fopen($file,"r");

$filesize=filesize($file);

$filetext=fread($f,$filesize);

echo $filetext;

fclose($f);

?> 

*****OUTPUT*****

will be the content of myfile.txt

GET Method: It is a superglobal variable. It is used to collect form data. It has a global scope and can be used anywhere in the program. But there is a problem with GET that it displays the form data into the URL(Uniform resource locator) or browser's Location therefore GET is not used in the case of the login form.

<!---PHP code-->

<?php

if(isset($_GET['btn']))

{

$n=$_GET['n'];

$r=$_GET['r'];

echo "Your Name is:".$n."<br>Your Rollno is: ".$r;

}

?>

<!--HTML Form-->

<form  method="get" enctype="multipart/form-data">

<table border="0" width="20%" cellpadding="5px" style="border:2px solid red">

<tr>

<td>Name</td>

<td><input type="text" name="n" /></td>

</tr>

<tr>

<td>Rollno</td>

<td><input type="text" name="r" /></td>

</tr>

<tr>

<td><input type="submit" name="btn" value="Click Here" /></td>

</tr>

</table>

</form>

*****OUTPUT*****

POST Method: It is a superglobal variable. It is used to collect form data. It has a global scope and can be used anywhere in the program. It does not display the form data into the URL(Uniform resource locator) or browser's Location. It provides security of data.

<!---PHP code-->

<?PHP

if(isset($_POST['but]))

{

$n=$_POST['n'];

$r=$_POST['r'];

echo "Your name is ".$n." and rollno is ".$r;

}

?>

<!--HTML Form-->

<form  method="post" enctype="multipart/form-data">

<table border="0" width="20%" cellpadding="5px" style="border:2px solid red">

<tr>

<td>Name</td>

<td><input type="text" name="n" /></td>

</tr>

<tr>

<td>Rollno</td>

<td><input type="text" name="r" /></td>

</tr>

<tr>

<td><input type="submit" name="btn" value="Click" /></td>

</tr>

</table>

</form>

Request Method: It is a superglobal variable. It is used to collect form data. It has a global scope and can be used anywhere in the program. But there is a problem with Request that it displays the form data into the URL(Uniform resource locator) or browser's Location therefore Request is not used in case of the login form. Request method same GET Method.

File Uploading in PHP

1. $_FILES is a superglobal variable that is used to upload files on the server.

2. The uploaded file could be a text file or image file or any document.

3. By using the $_FILES variable we can also get the file name, file size, file type, etc.

4. $_FILES['file']['tmp_name']: It is used to upload file in the temporary directory on the server.

5. $_FILES['file']['name']: It returns the name of uploaded file.

6. $_FILES['file']['size'] :It returns the size of uploaded file in bytes.

7. $_FILES['file']['type']:It returns the MIME type of the uploaded file.

How To Upload File in Php?

<!--PHP code-->

<?php

if(isset($_POST['btn']))

{

move_uploaded_file($_FILES['file']['tmp_name'],$_FILES['file']['name']);

}

?>

<!--HTML form-->

<form action=""  method="post" enctype="multipart/form-data">

<input type="file" name="file" >

<input type="submit" value="Upload" name="btn" />

</form>

Steps to upload a file

1. Run the above code on the server.

2. Click on the choose file button and selects a file to upload from the local computer.

3. Selected file is sent to the temporary directory on the server.

4. Now click the Upload button to upload the file on the server.

5. After that the selected file is uploaded to the same directory where you have saved your source code file


How To Printing Uploaded file details in Php?

<!--PHP code-->

<?php

if(isset($_POST['btn']))

{

move_uploaded_file($_FILES['file']['tmp_name'],$_FILES['file']['name']);

echo "File Name:".$_FILES['file']['name']."<br>";

echo "File Size:".$_FILES['file']['size']."<br>";

echo "File Type:".$_FILES['file']['type']."<br>";

}

?>

<!--HTML form-->

<form action=""  method="post" enctype="multipart/form-data">

<input type="file" name="file" >

<input type="submit" value="Upload" name="btn" />

</form>

Set limit on file size: Suppose that we want to upload a file that should not be larger than 80KB.

How To Set limit on file size in php?

<!--PHP code-->

<?php

if(isset($_POST['btn']))

{

/*Checking the file size*/

if($_FILES['file']['size']>80000)

{

 echo "file size is too large.";

}

else

{

move_uploaded_file($_FILES['file']['tmp_name'],$_FILES['file']['name']);

echo "File Name:".$_FILES['file']['name']."<br>";

echo "File Size:".$_FILES['file']['size']."<br>";

echo "File Type:".$_FILES['file']['type']."<br>";

}

}

?>

<!--HTML form-->

<form action=""  method="post" enctype="multipart/form-data">

<input type="file" name="file" >

<input type="submit" value="Upload" name="btn" />

</form>

Here if the file size is larger than 80KB you will get a message file size is too large.

Set limit on file type: Suppose we want to upload only jpg or png file.

How To set limit on file type in php?

<!--PHP code-->

<?php

if(isset($_POST['btn']))

{

/*Checking the file type*/

if($_FILES['file']['type']=="image/jpg" || $_FILES['file']['type']=="image/png")

{

move_uploaded_file($_FILES['file']['tmp_name'],$_FILES['file']['name']);

echo "File Name:".$_FILES['file']['name']."<br>";

echo "File Size:".$_FILES['file']['size']."<br>";

echo "File Type:".$_FILES['file']['type']."<br>";

}

else

{

echo "This file is not a jpg or png file.";

}

}

?>

<!--HTML form-->

<form action=""  method="post" enctype="multipart/form-data">

<input type="file" name="file">

<input type="submit" value="Upload" name="btn" />

</form>

Here you will get a message This file is not a jpg or png file if the uploaded file is not a png or jpg image.

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.