3

我有这个直接编写配置文件的 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;    

谢谢你。

4

2 回答 2

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);
于 2013-03-01T07:04:23.647 回答
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)
于 2013-03-01T07:29:55.420 回答