Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3324272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:24:06+00:00 2026-05-17T23:24:06+00:00

I need to store some data in an ini file, and I have run

  • 0

I need to store some data in an ini file, and I have run into a problem. It is not hard to read the dat from the ini file, as php provides a built in function for that:

<?php
ini_parse();
?>

The problem is that I need to save data to the INI file, while (preferably) preserving commments. I would especially like to preserve this comment at the top:

;<?php die(); ?>

Im sure you can guess the reason for this, but in case you can’t figure it out, I don’t want this file to be requested directly. I would just like to read the INI values out of it using another php script.

However if there is not a way to preserve the comments, I still need to store data into the INI file, so I still need a class to save data to the INI file.

Does anyone know of a class that might do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:24:07+00:00Added an answer on May 17, 2026 at 11:24 pm

    This comment won’t help you protect your file from being read in web browser, unless you configure your server to parse ini files as php source. Better idea is to place this file outside webroot, in password protected directory or configure one directory not to serve ini files.

    As for writing data, this function will do for simple arrays:

    function write_ini_file($file, array $options){
        $tmp = '';
        foreach($options as $section => $values){
            $tmp .= "[$section]\n";
            foreach($values as $key => $val){
                if(is_array($val)){
                    foreach($val as $k =>$v){
                        $tmp .= "{$key}[$k] = \"$v\"\n";
                    }
                }
                else
                    $tmp .= "$key = \"$val\"\n";
            }
            $tmp .= "\n";
        }
        file_put_contents($file, $tmp);
        unset($tmp);
    }
    

    So array like this:

    $options = array(
        'ftp_cfg' => array(
            'username' => 'user',
            'password' => 'pass',
            'hostname' => 'localhost',
            'port'     => 21
        ),
        'other_cfg' => array(
            'banned_emails' => array(
                'example@example.com',
                'spam@spam.gov'
            ),
            'ini_version' => 1.1
        )
    );
    

    Will be turned to:

    [ftp_cfg]
    username = "user"
    password = "pass"
    hostname = "localhost"
    port = "21"
    
    [other_cfg]
    banned_emails[0] = "example@example.com"
    banned_emails[1] = "spam@spam.gov"
    ini_version = "1.1"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store some data in a Django model. These data are not
I need to store some simple properties in a file and access them from
I usually use C++ stdlib map whenever I need to store some data associated
I need to store some sensitive data by encrypting it with atleast 128 bit
I'm getting some objects from an external library and I need to store those
I need debug some old code that uses a Hashtable to store response from
I need to store some values in the database, distance, weight etc. In my
I'm using C# and .NET 3.5. I need to generate and store some T-SQL
[edit] We're collecting credit application data from users on a web form. I have
So I am importing some images stored in SQL image columns, and I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.