Jump to content

Array not writing to file as expected...


AncientSage

Recommended Posts

I'm serializing the array, and all, but it's writing it back to the text file as "Array".

[code]

  $textfile = "filelist.txt";
  if (file_exists($textfile) && is_file($textfile)) {
  $fp = fopen($textfile, "r");
  $file = file_get_contents($textfile, False, NULL);
  $rows = explode("\n", $file);
    if(isset($_POST['checksubmit'])) {
        if(isset($_POST['row']))
          foreach($_POST['row'] as $i)
              unset($rows[$i]);
      serialize($rows);
      $fpw = fopen($textfile, "w");
      flock($fpw, LOCK_EX);
      fwrite($fpw, $rows);
      flock($fpw, LOCK_UN); 
      }
    }
  echo "<form action=\"" . append_sid("admin.php") . "\" method=\"POST\">";
      foreach($rows as $x => $row) {
        if($row == ""){
          continue;
        }
      echo '<input type="checkbox" name="row[]" value="'. $x .'">' . $row . "<br /> ";
      }
  echo "<input type=\"hidden\" name=\"checksubmit\">";
  echo "<br /><input type=\"submit\" value=\"Delete Selected\">";
  echo "</form>";
  }
[/code]

Now, as you can see, I'm using explode() on part of the array, not sure it that could cause part of the problem. Anyway, what is wrong in this code that would be causing the array to keep writing back as Array?
Link to comment
https://forums.phpfreaks.com/topic/15224-array-not-writing-to-file-as-expected/
Share on other sites

i have a feeling that it's because you're not returning the serial to anything.  serialize() doesn't work directly on the variable, it returns the serialized version.  try this:

[code]$serialized_array = serialize($rows);
...
fwrite($fpw, $serialized_array);[/code]

haven't used serialize before, so this is a guess.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.