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

PHP - Insert Data to INI File

chile666

Loot Collector
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
122
Likes
27
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Insert Data to INI File 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...

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.

Lastly, 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. <?php

    require

    'generate_ini.php'

    ?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Insert Data to INI File</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" action="create_data.php" class="form-inline">
  19. <label>Enter a Data</label>
  20. <input type="text" class="form-control" name="data" required="required"/>
  21. <button class="btn btn-success" name="add"><span class="glyphicon glyphicon-plus"></span> Add Data</button>
  22. </form>
  23. <br />
  24. <table class="table table-bordered">
  25. <thead class="alert-info">
  26. <?php
  27. $file

    =

    "config.ini"

    ;
  28. $conf

    =

    file_get_contents

    (

    $file

    )

    ;
  29. $replace

    =

    preg_replace

    (

    "/\(|\)/"

    ,

    ""

    ,

    $conf

    )

    ;
  30. $section

    =

    explode

    (

    '# Manual Settings'

    ,

    $replace

    )

    ;
  31. $parse

    =

    parse_ini_string(

    $section

    [

    0

    ]

    ,

    false

    ,

    INI_SCANNER_RAW)

    ;
  32. ?>
  33. <tr>
  34. <th colspan="2"><center>Start Settings</center></th>
  35. </tr>
  36. </thead>
  37. <tbody style="background-color:#fff;">
  38. <?php
  39. foreach

    (

    $parse

    as

    $data

    =>

    $list

    )

    {
  40. ?>
  41. <tr>
  42. <td><?php

    echo

    $data

    ?>

    </td>
  43. <td><?php

    echo

    $list

    ?>

    </td>
  44. </tr>
  45. <?php
  46. }
  47. ?>
  48. </tbody>
  49. </table>
  50. <table class="table table-bordered">
  51. <thead class="alert-warning">
  52. <tr>
  53. <th colspan="2"><center>Manual Settings</center></th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <?php
  58. $parse2

    =

    parse_ini_string(

    $section

    [

    1

    ]

    ,

    true

    ,

    INI_SCANNER_RAW)

    ;
  59. foreach

    (

    $parse2

    as

    $head

    =>

    $list

    )

    {
  60. ?>
  61. <tr class="alert-success">
  62. <td colspan="2"><center><?php

    echo

    $head

    ?>

    </center></td>
  63. </tr>
  64. <?php
  65. foreach

    (

    $list

    as

    $key

    =>

    $value

    )

    {
  66. ?>
  67. <tr style="background-color:#fff;">
  68. <td><?php

    echo

    $key

    ?>

    </td>
  69. <td><?php

    echo

    $value

    ?>

    </td>
  70. </tr>

  71. <?php
  72. }
  73. }
  74. ?>
  75. </tbody>
  76. </table>
  77. </div>

  78. </body>
  79. </html>

Creating the Ini File

This code contains a function for creating the ini file. This code will generate an ini file to the corresponded directory. To do that just copy and write these block of codes inside the text editor, then save it as generate_ini.php
  1. <?php
  2. if

    (

    !

    file_exists

    (

    'config.ini'

    )

    )

    {
  3. $file

    =

    "./config.ini"

    ;
  4. $array

    =

    array

    (
  5. "# Start Settings"

    ,
  6. "system_clean=true"

    ,
  7. "system_read_data=ALL"

    ,
  8. "system_count=10"

    ,
  9. ""

    ,
  10. "# Manual Settings"
  11. )

    ;

  12. file_put_contents

    (

    $file

    ,

    implode

    (

    PHP_EOL,

    $array

    )

    ,

    FILE_APPEND)

    ;
  13. }
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will save the data inputs to the ini file. To this just copy and write down these codes inside the text editor, then save it as create_data.php
  1. <?php
  2. ob_start

    (

    )

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'add'

    ]

    )

    )

    {
  4. $file

    =

    "config.ini"

    ;
  5. $conf

    =

    file_get_contents

    (

    $file

    )

    ;
  6. $replace

    =

    preg_replace

    (

    "/\(|\)/"

    ,

    ""

    ,

    $conf

    )

    ;
  7. $section

    =

    explode

    (

    '# Manual Settings'

    ,

    $replace

    )

    ;
  8. $parse

    =

    parse_ini_string(

    $section

    [

    0

    ]

    ,

    false

    ,

    INI_SCANNER_RAW)

    ;
  9. $content

    =

    ""

    ;

  10. $new_array

    =

    array

    (
  11. ""

    ,
  12. "["

    .

    $_POST

    [

    'data'

    ]

    .

    "]"

    ,
  13. strtolower

    (

    $_POST

    [

    'data'

    ]

    )

    .

    "_allow_local_infile=on"

    ,
  14. strtolower

    (

    $_POST

    [

    'data'

    ]

    )

    .

    "_allow_persistent=on"

    ,
  15. strtolower

    (

    $_POST

    [

    'data'

    ]

    )

    .

    "_cache_size=2000"

    ,
  16. )

    ;




  17. $content

    .=

    "# Start Settings\n

    "

    ;
  18. foreach

    (

    $parse

    as

    $head

    =>

    $list

    )

    {
  19. $content

    .=

    $head

    .

    "="

    .

    $list

    .

    "\n

    "

    ;
  20. }

  21. $parse2

    =

    parse_ini_string(

    $section

    [

    1

    ]

    ,

    true

    ,

    INI_SCANNER_RAW)

    ;

  22. $content

    .=

    "\n

    # Manual Settings\n

    "

    ;

  23. foreach

    (

    $parse2

    as

    $head

    =>

    $list

    )

    {
  24. $content

    .=

    "\n

    ["

    .

    $head

    .

    "]\n

    "

    ;
  25. foreach

    (

    $list

    as

    $key

    =>

    $value

    )

    {
  26. $content

    .=

    $key

    .

    "="

    .

    $value

    .

    "\n

    "

    ;
  27. }
  28. }

  29. foreach

    (

    $new_array

    as

    $value

    )

    {
  30. $content

    .=

    $value

    .

    "\n

    "

    ;
  31. }


  32. if

    (

    !

    $handle

    =

    fopen

    (

    $file

    ,

    'w'

    )

    )

    {
  33. return

    false

    ;
  34. }

  35. fwrite

    (

    $handle

    ,

    $content

    )

    ;
  36. fclose

    (

    $handle

    )

    ;


  37. header

    (

    "location: index.php"

    )

    ;

  38. }
  39. ?>

⚙ Live Demo

There you have it we successfully created Insert Data to INI File 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,496

334,779

334,787

Top