Array in define() speichern

Hallo zusammen,
ich habe ein script das eine config datei erstellt und mehre Konstanten definiert mit placeholdern
Das funktioniert aber nicht. Hat einer eine Idee wie man das macht?

script:
PHP:
  <?php
  $q = $_GET["q"];
  $data = array("LANGUAGE" => "%LANGUAGE%",
                        "DB_HOST" => "%DB_HOST%",
                        "DB_USER" => "%DB_USER%",
                        "DB_PASS" => "%DB_PASS%"
 );
   $includedatei = "config.php";
   $include = @fopen($includedatei, "a+");

 foreach ($data as $key => $value) {
   $includedatei = "config.php";
   $include = @fopen($includedatei, "w+");
   $includetext = "define('".$key."', '".$value."');";
   echo $includetext;
     @fputs($include, "$includetext");
 }
   @fclose($include);
//Template einlesen
$temp = "config.php";
$includetemplate= fopen ($temp,"r+");
$includetext = file_get_contents($temp);
$result =  str_replace("%LANGUAGE%",$q,$includetext);
$res = fputs($includetemplate, $result);
fclose($includetemplate);
if ($res) {
  echo "Alles Super";
}
?>
 
Das Problem ist, dass Du innerhalb der Schleife ein fopen machst. Dadurch gelangt nur der letzte Eintrag in die Datei.
Ich würde dies jedoch einfacher aufziehen: In einem Durchgang das Array lesen, den Platzhalter ersetzen und die Zeile wegschreiben:
PHP:
$q = $_GET["q"];
$data = array("LANGUAGE" => "%LANGUAGE%",
    "DB_HOST" => "%DB_HOST%",
    "DB_USER" => "%DB_USER%",
    "DB_PASS" => "%DB_PASS%",
);
$configfile = "config2.php";
@unlink($configfile);
foreach ($data as $key => $value) {
    $value = str_replace("%LANGUAGE%", $q, $value);
    $text = "define('" . $key . "', '" . $value . "');" . PHP_EOL;
    file_put_contents($configfile, $text, FILE_APPEND);
}
 

Neue Beiträge

Zurück