PHP - Simple File Handling
Submitted by razormist on Tuesday, May 15, 2018 - 19:51.
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.
add_file.php
edit_file.php
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";
- 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
- }
- ?>
- </tbody>
- </table>
- </div>
- </body>
- </html>
- <!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";
- ?>
- <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>
- <!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";
- ?>
- <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>