Home  >  Article  >  Backend Development  >  Summarize two methods for modifying the contents of ini files in PHP

Summarize two methods for modifying the contents of ini files in PHP

PHPz
PHPzOriginal
2023-04-03 17:55:142079browse

Recently, I encountered a problem. I needed to use PHP to modify the contents of several INI files. However, I searched a lot of information on the Internet at first, but could not find a good solution. After some attempts, I summarized some better methods, hoping to be helpful to PHP developers who need to modify the contents of INI files.

The INI file is a configuration file that is common in many software. Under normal circumstances, INI files are not a mainstream format, but its configuration method is very useful, so many developers will use INI files to perform some basic configurations when using software. INI files are also used in some places in website development, such as the PHP.ini file. By modifying some parameters in this file, you can modify some PHP runtime configuration options. So, let's take a look at how to modify the contents of the INI file through PHP.

Method 1: Use the fopen function to open the file for modification

This method is relatively primitive. You need to obtain the handle of the file, then write the modified content to the file through the fwrite function, and finally Then close the file handle.

For example, if we want to modify the max_execution_time parameter in the PHP.ini file, we can write like this:

$file = fopen('/path/to/php.ini', 'r+') or die('open file failed');

while (($line = fgets($file)) !== false)
{
    if (strpos($line, 'max_execution_time') === 0)
    {
        $line = 'max_execution_time = 300'."\r\n";
    }
    $new_content .= $line;
}

fseek($file, 0); // 将文件指针指向文件头
fwrite($file, $new_content);
fclose($file);

Although this method is simple to operate, it may be too large for the size and content of the file. It will be more dangerous because we have to read the entire file and write the modified content to the file at once. If the file is too large, it may cause some problems such as insufficient PHP memory or timeout. At this time, we can use a better way to deal with it.

Method 2: Use parse_ini_file and parse_ini_string functions

This method is relatively simple. We can directly load the INI file into an array through the parse_ini_file function, and then modify a parameter in the array. , and then use the parse_ini_string function to convert the array into an INI format string, and finally write the string to the file.

For example, if we want to modify the max_execution_time parameter in the PHP.ini file, we can write like this:

$config = parse_ini_file('/path/to/php.ini');
$config['max_execution_time'] = 300;
$new_content = '';
foreach ($config as $key => $value)
{
    $new_content .= $key.' = '.$value."\r\n";
}

file_put_contents('/path/to/php.ini', $new_content);

For large files, you can also operate in the same way as above, so you don’t have to The entire file is read into memory.

Specify a specific INI file

Both of the above methods use the system default INI file by default. If you need to specify a specific INI file, you can use the ini_set function to modify it.

For example:

ini_set('memory_limit', '-1'); // 指定自定义的INI文件

Summary

Modifying INI files is an inevitable problem in PHP development. Through the above two methods, you can easily complete the modification of INI files. modification operation. However, it should be noted that if the INI file to be modified is large, the second method should be used to reduce memory usage. At the same time, we can also specify a specific INI file to operate through the ini_set function.

The above is the detailed content of Summarize two methods for modifying the contents of ini files in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn