killerabgg
Compiler Tuner
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
100 XP
In this tutorial we will create a File Update using PHP. This code can create a text file with content dynamically when user submit the form inputs. The code use PHP POST to launch a specific method that update a text using these special php functions fwrite() to rewrite an existing file and lastly fclose() safely close the connection between the opened file. This is a user-friendly kind of program feel free to modify it.
We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.
Getting 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.
And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.
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 your text editor, then save it as index.php.
Creating the Main Function
This code contains the main function of the application. This code will update a text file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.
add_file.php
edit_file.php
save_file.php
There you have it we successfully created File Update 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
We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.
Getting 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.
And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.
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 your text editor, then save it as 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">File Update Using PHP Source Code</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div>
- <div class="col-md-4">
- <form method="POST" action="add_file.php">
- <div class="form-group">
- <label>Filename:</label>
- <input type="text" name="name" class="form-control"/>
- </div>
- <center><button class="btn btn-primary">Create File</button></center>
- </form>
- </div>
- <div class="col-md-8">
- <table class="table table-bordered">
- <thead class="alert-info">
- <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></center></td>
- </tr>
- <?php
- }
- closedir
(
$dir
)
;
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </html>
Creating the Main Function
This code contains the main function of the application. This code will update a text file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.
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">File Update Using PHP Source Code</h3>
- <hr style="border-top:1px dotted #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">File Update Using PHP Source Code</h3>
- <hr style="border-top:1px dotted #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-warning">Update</button>
- </form>
- </div>
- </body>
- </html>
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 File Update 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 the hidden content.