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