PHP - Simple File Handling

Submitted by: 
Language: 
Visitors have accessed this post 713 times.

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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple File Handling</h3>
  17. <hr style="border-top:1px dottec #ccc;"/>
  18. <div class="form-inline">
  19. <form method="POST" action="add_file.php">
  20. <label style="font-size:18px;">Filename:</label>
  21. <input type="text" name="name" class="form-control"/>
  22. <button type="submit" class="btn btn-primary">Create File</button>
  23. </form>
  24. </div>
  25. <br />
  26. <table class="table table-bordered">
  27. <thead>
  28. <tr>
  29. <th>Name</th>
  30. <th>Action</th>
  31. </tr>
  32. </thead>
  33. <tbody>
  34. <?php
  35. $full_path = "files";
  36.  
  37. $dir = opendir($full_path);
  38.  
  39. while(($file = readdir($dir)) !== FALSE){
  40. if($file == "." || $file == "..")
  41.  
  42. continue;
  43. ?>
  44. <tr>
  45. <td><?php echo $file?></td>
  46. <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>
  47. </tr>
  48. <?php
  49.  
  50. }
  51.  
  52. closedir($dir);
  53. ?>
  54.  
  55. </tbody>
  56. </table>
  57. </div>
  58. </body>
  59. </html>

add_file.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple File Handling</h3>
  17. <hr style="border-top:1px dottec #ccc;"/>
  18. <?php
  19. $path = "files";
  20. $filename = $_POST['name'].".txt";
  21. fopen($path."/".$filename, 'w');
  22. ?>
  23. <form method="POST" action="save_file.php">
  24. <label>Enter a Text</label>
  25. <textarea name="content" style="width:100%; height:100px; resize:none;"></textarea>
  26. <input type="hidden" name="filename" value="<?php echo $filename?>"/>
  27. <button type="submit" class="btn btn-primary">Save</button>
  28. </form>
  29. </div>
  30. </body>
  31. </html>

edit_file.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple File Handling</h3>
  17. <hr style="border-top:1px dottec #ccc;"/>
  18. <?php
  19. $file_name = $_GET['filename'];
  20. $path = "files";
  21. $file = fopen($path."/".$file_name,"r");
  22. $content = fread($file, filesize($path."/".$file_name));
  23. fclose($file);
  24.  
  25. ?>
  26. <form method="POST" action="save_file.php">
  27. <label>Enter a Text</label>
  28. <textarea name="content" style="width:100%; height:100px; resize:none;"><?php echo $content ?></textarea>
  29. <input type="hidden" name="filename" value="<?php echo $file_name?>"/>
  30. <button type="submit" class="btn btn-primary">Save</button>
  31. </form>
  32. </div>
  33. </body>
  34. </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.

  1. <?php
  2.  
  3. if($_POST['content'] != ""){
  4. $content = $_POST['content'];
  5. $file_name = $_POST['filename'];
  6. $path = "files";
  7. $file = fopen($path."/".$file_name, 'w');
  8. fwrite($file, $content);
  9. fclose($file);
  10. header("location:index.php");
  11. }else{
  12. echo "Error!";
  13. echo "<a href='index.php'>Back</a>";
  14.  
  15. }
  16. ?>

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!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.