LisaW
LisaW

Reputation: 173

fWrite overwrites the whole file?

me again, this time with a PHP question... I'm busy making some sort of admin panel (Really basic) but when I'm trying to change like my slider or something the fwrite overwrites EVERYTHING. This is my PHP code:

 <?php 
          $errorsslider = array();
          if($_POST['slider']){
              $slider_site = $_POST['slider'];
           $f=fopen("../app/includes/configfunctions.php","w");

   $set_slider = 
   "<?php
    DEFINE('slider', " . $_POST['slider'] . ");
     ";
  if (fwrite($f,$set_slider)>0){
   fclose($f);

  }
  $errorsslider[] = "<div class='alert alert-success'><button type='button' class='close' data-dismiss='alert'>x</button>" . $LANG['admin']['global']['SliderSuccess']; "</div>";
        return $errorsslider;
          }
  ?>

So before I run this, this is my file:

<?php
DEFINE('slider', false);
DEFINE('login', true);
?>

and after I runned it, this is my file:

<?php
DEFINE('slider', false);
?>

Does anyone have a solution?

P.S. Don't mind the code, looks ugly, I know...

Lisa

Upvotes: 3

Views: 6896

Answers (1)

h2ooooooo
h2ooooooo

Reputation: 39550

The documentation of fopen() specifies w being:

'w'
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

Hence your file is completely replaced every time. If you want to append to the file, open it with a instead (fopen($file, 'a')):

'a'
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

Upvotes: 7

Related Questions