Jump to content


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


Array not writing to file as expected...

Recommended Posts

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


  $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'])) {
          foreach($_POST['row'] as $i)
      $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 == ""){
      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>";

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?

Share this post

Link to post
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.

Share this post

Link to post
Share on other sites
+1 rule of PHP I did not know (or more so, of the serialize() function).

I guess that would be my problem then, as it is storing it as it should now.


Share this post

Link to post
Share on other sites


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.