使用变量更新config.php

时间:2013-03-01 06:55:35

标签: php

我有直接编写配置文件的php设置,我想更新文件中的变量而不是编写整个文件。 有人能指出我正确的方向吗?

  $allowedExts = array(".jpg", ".jpeg", ".gif", ".png");
  $extension = strrchr ($_FILES["logoFile"]['name'], '.');

  if (($_FILES["logoFile"]["size"] < 2000000)
  && in_array($extension, $allowedExts))
  {
if ($_FILES["logoFile"]["error"] > 0)
{
    echo "Return Code: " . $_FILES["logoFile"]["error"] . "<br />";
}
else
{

move_uploaded_file($_FILES["logoFile"]["tmp_name"],"../../upload/logo".$extension);



$path= "./upload/logo".$extension;

//update logo settings variable

$data = file_get_contents('../settings/settings.php'); // grab data
$data = str_replace("/'LOGO_IMAGE_PATH', '[^']+'/", "'LOGO_IMAGE_PATH', '$path'", $data); // replace that section
file_put_contents('../settings/settings.php', $data); // save data


}
}
else
{
    echo "Invalid file";
}

其余变量来自saveSettings。

case "saveSettings":

        //Logo settings
        $showLogo=$_REQUEST["showLogoOption"];
        $roundOption=$_REQUEST["roundOption"];
        $siteTitle=$_REQUEST["siteTitle"];
        $requireJobTypes=$_REQUEST["requireJobTypes"];
        $enableJobTypes=$_REQUEST["useJobTypes"];
        $enableMileage=$_REQUEST["enableMileage"];
        $allowViewingPayrolls=$_REQUEST["allowViewingPayrolls"];
        $trackIP=$_REQUEST["trackIP"];
        $enableMobile=$_REQUEST["enableMobile"];
        $config="<?php 
           define('USE_LOGO_IMAGE', $showLogo);
           define('ROUND_TO_QUARTERS', $roundOption);
           define('SITE_TITLE', '$siteTitle');
           define('ENABLE_JOB_TYPES', $enableJobTypes);
           define('REQUIRE_JOB_TYPES', $requireJobTypes);
           define('ENABLE_MILEAGE', $enableMileage);
           define('ALLOW_VIEW_PAYROLLS', $allowViewingPayrolls);
           define('TRACK_IP', $trackIP);
           define('MOBILE_VERSION_ENABLED', $enableMobile);
        ?>";
        $fp = fopen("../settings/settings.php", "w");
        fwrite($fp, $config);
        fclose($fp);
        break;    

谢谢。

2 个答案:

答案 0 :(得分:1)

这是为您更新设置的功能,它将执行此操作,因此您的settings.php文件仅在设置过程中被修改一次,因此不会为每个设置不必要地打开文件。它将一举拯救它们。

function save_settings($settings) // function to update settings to file
{
    $data = file('../settings/settings.php', FILE_SKIP_EMPTY_LINES); // grab current settings file

    foreach ($data as $key => $line) // loop through lines in file
    {
        foreach ($settings as $name => $value) // loop through settings that are being modified
        {
            if (strcasecmp('true', $value) && strcasecmp('false', $value)
            && '0' !== $value && '1' !== $value)
            {
                $value = "'" . htmlspecialchars($value, ENT_QUOTES) . "'"; // quote non-boolean, also escape quotes
            }

            if (FALSE !== strpos($line, $name)) // if exists update it
            {
                $data[$key] = "define('" . $name . "', " . $value . ");\n"; 
            }
        }
    }
    file_put_contents('../settings/settings.php', implode('', $data)); // save it all
}

将此行放在设置过程的开头

// run this line BEFORE you begin to set values in settings.php to initialize the settings array
$settings = array();

然后您可以修改设置:

// update settings easily by doing the below anywhere you like
// as long as it is after the above line and before you run save_settings()
$settings['LOGO_IMAGE_PATH']        = $path;
$settings['USE_LOGO_IMAGE']         = $_POST['showLogoOption'];
$settings['ROUND_TO_QUARTERS']      = $_POST['roundOption'];
$settings['SITE_TITLE']             = $_POST['siteTitle'];
$settings['ENABLE_JOB_TYPES']       = $_POST['requireJobTypes'];
$settings['REQUIRE_JOB_TYPES']      = $_POST['useJobTypes'];
$settings['ENABLE_MILEAGE']         = $_POST['enableMileage'];
$settings['ALLOW_VIEW_PAYROLLS']    = $_POST['allowViewingPayrolls'];
$settings['TRACK_IP']               = $_POST['trackIP'];
$settings['MOBILE_VERSION_ENABLED'] = $_POST['enableMobile'];

然后,当您完成所有操作后,通过使用更新的设置保存设置文件来最终确定设置文件:

// run this line at the end to save ALL settings AFTER you are done setting their values
save_settings($settings);

答案 1 :(得分:0)

你可以做这样的事情。

//传递数组以进行更新

function update_config($myConfig) {
  $myConfig = json_encode($myConfig);
  $fp = fopen('my_config.ini', 'a');
  fwrite($fp, $myConfig);
  return 'success';
}

//在需要时更新配置

$myConfig = array();
$myConfig['section_1'] = "a";
$myConfig['section_2'] = "b";
$myConfig['section_3'] = "c";
echo update_config($myConfig)