• We just launched and are currently in beta. Join us as we build and grow the community.

File Update Using PHP Source Code

killerabgg

Compiler Tuner
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
203
Likes
111
Bits
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.
  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="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">File Update Using PHP Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div>
  18. <div class="col-md-4">
  19. <form method="POST" action="add_file.php">
  20. <div class="form-group">
  21. <label>Filename:</label>
  22. <input type="text" name="name" class="form-control"/>
  23. </div>
  24. <center><button class="btn btn-primary">Create File</button></center>
  25. </form>
  26. </div>
  27. <div class="col-md-8">
  28. <table class="table table-bordered">
  29. <thead class="alert-info">
  30. <tr>
  31. <th>Name</th>
  32. <th>Action</th>
  33. </tr>
  34. </thead>
  35. <tbody>
  36. <?php
  37. $full_path

    =

    "files"

    ;

  38. $dir

    =

    opendir

    (

    $full_path

    )

    ;

  39. while

    (

    (

    $file

    =

    readdir

    (

    $dir

    )

    )

    !==

    FALSE

    )

    {
  40. if

    (

    $file

    ==

    "."

    ||

    $file

    ==

    ".."

    )

  41. continue

    ;
  42. ?>
  43. <tr>
  44. <td><?php

    echo

    $file

    ?>

    </td>
  45. <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>
  46. </tr>
  47. <?php

  48. }

  49. closedir

    (

    $dir

    )

    ;
  50. ?>

  51. </tbody>
  52. </table>
  53. </div>
  54. </div>


  55. </body>
  56. </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
  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="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">File Update Using PHP Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <?php
  18. $path

    =

    "files"

    ;
  19. $filename

    =

    $_POST

    [

    'name'

    ]

    .

    ".txt"

    ;
  20. fopen

    (

    $path

    .

    "/"

    .

    $filename

    ,

    'w'

    )

    ;
  21. ?>
  22. <form method="POST" action="save_file.php">
  23. <label>Enter a Text</label>
  24. <textarea name="content" style="width:100%; height:100px; resize:none;"></textarea>
  25. <input type="hidden" name="filename" value="<?php

    echo

    $filename

    ?>

    "/>
  26. <button type="submit" class="btn btn-primary">Save</button>
  27. </form>
  28. </div>


  29. </body>
  30. </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="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">File Update Using PHP Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <?php
  18. $file_name

    =

    $_GET

    [

    'filename'

    ]

    ;
  19. $path

    =

    "files"

    ;
  20. $file

    =

    fopen

    (

    $path

    .

    "/"

    .

    $file_name

    ,

    "r"

    )

    ;
  21. $content

    =

    fread

    (

    $file

    ,

    filesize

    (

    $path

    .

    "/"

    .

    $file_name

    )

    )

    ;
  22. fclose

    (

    $file

    )

    ;

  23. ?>
  24. <form method="POST" action="save_file.php">
  25. <label>Enter a Text</label>
  26. <textarea name="content" style="width:100%; height:100px; resize:none;"><?php

    echo

    $content

    ?>

    </textarea>
  27. <input type="hidden" name="filename" value="<?php

    echo

    $file_name

    ?>

    "/>
  28. <button type="submit" class="btn btn-warning">Update</button>
  29. </form>
  30. </div>


  31. </body>
  32. </html>

save_file.php
  1. <?php

  2. if

    (

    $_POST

    [

    'content'

    ]

    !=

    ""

    )

    {
  3. $content

    =

    $_POST

    [

    'content'

    ]

    ;
  4. $file_name

    =

    $_POST

    [

    'filename'

    ]

    ;
  5. $path

    =

    "files"

    ;
  6. $file

    =

    fopen

    (

    $path

    .

    "/"

    .

    $file_name

    ,

    'w'

    )

    ;
  7. fwrite

    (

    $file

    ,

    $content

    )

    ;
  8. fclose

    (

    $file

    )

    ;
  9. header

    (

    "location:index.php"

    )

    ;
  10. }

    else

    {
  11. echo

    "Error!"

    ;
  12. echo

    "<a href='index.php'>Back</a>"

    ;

  13. }

  14. ?>

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.
 

452,292

324,736

324,744

Top