PHP File Handling Functions
Code for getting input from a form and writing data to a file
<!--Writing to a simple file using fwrite-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Handling using PHP</title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<label for="regnumber">Enter your Registration Number</label>
<input type="text" required name="regnumber" id="regnumber"> <br>
<label for="Name">Enter your Name</label>
<input type="text" required name="username" id="username"> <br>
<label for="comments">Enter your Comments</label> <br>
<textarea name="comments" id="comments" cols="30" rows="10"></textarea> <br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$regnumber = $_POST['regnumber'];
$username = $_POST['username'];
$comments = $_POST['comments'];
$myfile = fopen("satish.txt","w") or die("Cant open the file");
$txt=$regnumber.$username.$comments;
fwrite($myfile,$txt);
fclose($myfile);
}
?>
</body>
</html>
Output
Code for getting input from a form and appending data to a file
<!--appending to a simple file using fwrite-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Handling using PHP</title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<label for="regnumber">Enter your Registration Number</label>
<input type="text" required name="regnumber" id="regnumber"> <br>
<label for="Name">Enter your Name</label>
<input type="text" required name="username" id="username"> <br>
<label for="comments">Enter your Comments</label> <br>
<textarea name="comments" id="comments" cols="30" rows="10"></textarea> <br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$regnumber = $_POST['regnumber'];
$username = $_POST['username'];
$comments = $_POST['comments'];
$myfile = fopen("satish.txt","a") or die("Cant open the file");
$txt=$regnumber.$username.$comments;
fwrite($myfile,$txt);
fclose($myfile);
}
?>
</body>
</html>
Output
Reading from a file and displaying the data to the user.
<!--reading data from an existing file-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Handling using PHP</title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<label for="regnumber">Click on view contents to read the contents from the file</label>
<input type="submit" name="submit" value="View Contents">
</form>
<?php
if(isset($_POST['submit']))
{
$myfile = fopen("satish.txt","r") or die("Cant open the file");
$txt= fread($myfile,filesize("satish.txt"));
echo $txt;
fclose($myfile);
}
?>
</body>
</html>
Output
Code demonstrating file handling methods
<?php
$myfile = fopen("satish.txt","r") or die("Cant open the file");
if(file_exists("satish.txt"))
{
echo "File already Exists"."<br>";
}
if(is_readable("satish.txt"))
{
echo "file is readable"."<br>";
}
echo basename("C:\xampp\htdocs\iwp\satish.txt"."<br>");
echo dirname("C:\xampp\htdocs\iwp\satish.txt");echo "<br>";
echo fgetc($myfile); echo "<br>";
echo fgets($myfile);echo "<br>";
echo filesize("satish.txt");echo "<br>";
echo filetype("satish.txt");echo "<br>";
$text = file_get_contents("satish.txt");
copy("satish.txt","satishcopy.txt");
// unlink("satishcopy.txt"); //will delete the file
echo $text;echo "<br>";
fseek($myfile,4);
echo fgets($myfile);
?>
Output