rexi_404
Blockchain Policy Advocate
Divine
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
In this tutorial we will create a Simple File Handling using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.
Before we started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as shown below.
index.php
add_file.php
edit_file.php
Creating the Save Function
This code contains the specific script for the save function. This code will save the file by sending the post data to the file. To do that write these block of codes inside the Text editor and call it as save_file.php.
There you have it we successfully created a Simple File Handling using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Download
Before we started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as shown below.
index.php
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Simple File Handling</h3>
- <hr style="border-top:1px dottec #ccc;"/>
- <div class="form-inline">
- <form method="POST" action="add_file.php">
- <label style="font-size:18px;">Filename:</label>
- <input type="text" name="name" class="form-control"/>
- <button type="submit" class="btn btn-primary">Create File</button>
- </form>
- </div>
- <br />
- <table class="table table-bordered">
- <thead>
- <tr>
- <th>Name</th>
- <th>Action</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $full_path
=
"files"
;
- $dir
=
opendir
(
$full_path
)
;
- while
(
(
$file
=
readdir
(
$dir
)
)
!==
FALSE
)
{
- if
(
$file
==
"."
||
$file
==
".."
)
- continue
;
- ?>
- <tr>
- <td><?php
echo
$file
?>
</td>
- <td colspan="2"><center><a class="btn btn-warning" href="edit_file.php?filename=<?php
echo
$file
?>
"><span class="glyphicon glyphicon-edit"></span> Edit</a> <a class="btn btn-danger" href=""><span class="glyphicon glyphicon-trash"></span> Delete</a></center></td>
- </tr>
- <?php
- }
- closedir
(
$dir
)
;
- ?>
- </tbody>
- </table>
- </div>
- </body>
- </html>
add_file.php
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Simple File Handling</h3>
- <hr style="border-top:1px dottec #ccc;"/>
- <?php
- $path
=
"files"
;
- $filename
=
$_POST
[
'name'
]
.
".txt"
;
- fopen
(
$path
.
"/"
.
$filename
,
'w'
)
;
- ?>
- <form method="POST" action="save_file.php">
- <label>Enter a Text</label>
- <textarea name="content" style="width:100%; height:100px; resize:none;"></textarea>
- <input type="hidden" name="filename" value="<?php
echo
$filename
?>
"/>
- <button type="submit" class="btn btn-primary">Save</button>
- </form>
- </div>
- </body>
- </html>
edit_file.php
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Simple File Handling</h3>
- <hr style="border-top:1px dottec #ccc;"/>
- <?php
- $file_name
=
$_GET
[
'filename'
]
;
- $path
=
"files"
;
- $file
=
fopen
(
$path
.
"/"
.
$file_name
,
"r"
)
;
- $content
=
fread
(
$file
,
filesize
(
$path
.
"/"
.
$file_name
)
)
;
- fclose
(
$file
)
;
- ?>
- <form method="POST" action="save_file.php">
- <label>Enter a Text</label>
- <textarea name="content" style="width:100%; height:100px; resize:none;"><?php
echo
$content
?>
</textarea>
- <input type="hidden" name="filename" value="<?php
echo
$file_name
?>
"/>
- <button type="submit" class="btn btn-primary">Save</button>
- </form>
- </div>
- </body>
- </html>
Creating the Save Function
This code contains the specific script for the save function. This code will save the file by sending the post data to the file. To do that write these block of codes inside the Text editor and call it as save_file.php.
- <?php
- if
(
$_POST
[
'content'
]
!=
""
)
{
- $content
=
$_POST
[
'content'
]
;
- $file_name
=
$_POST
[
'filename'
]
;
- $path
=
"files"
;
- $file
=
fopen
(
$path
.
"/"
.
$file_name
,
'w'
)
;
- fwrite
(
$file
,
$content
)
;
- fclose
(
$file
)
;
- header
(
"location:index.php"
)
;
- }
else
{
- echo
"Error!"
;
- echo
"<a href='index.php'>Back</a>"
;
- }
- ?>
There you have it we successfully created a Simple File Handling using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Download
You must upgrade your account or reply in the thread to view hidden text.