Jump to content


Photo

Array not writing to file as expected...


  • Please log in to reply
2 replies to this topic

#1 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 21 July 2006 - 05:32 AM

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'])) {
        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>";
   }

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?

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 21 July 2006 - 05:40 AM

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:

$serialized_array = serialize($rows);
...
fwrite($fpw, $serialized_array);

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

#3 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 21 July 2006 - 05:47 AM

+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.

Thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users