Page 1 of 1

Writing Array To File

Posted: Mon Aug 17, 2009 2:36 pm
by Monotoko
Hello fellow developers...i was wondering if i could get some of your insight into this problem i appear to be having

I need to know how to save an array into a .txt document as i cannot figure out how to do it.

Here is what i have so far:

Code: Select all

<?php
//Hold server IP
$ip_hold = $_SERVER['SERVER_ADDR'];
$ip_write = "The IP of the server that send this log is ".$ip_hold."";
//Open www directory
$dir = opendir("../../");
$count = 0;
$count2 = 0;
 
//Put all folders into an array
while (($file = readdir($dir)) !== false)
{
$files[$count]=$file;
$count++;
}
closedir($dir);
 
while ($count2 != $count)
{
$temp = $files[$count2];
$direc = "../../$temp";
 
if(is_dir($direc))
    {
        $filename = "../../$temp/pubstermx/inc/pubsterdomainconfig.php";
        if (file_exists($filename))
                {
            require $filename;
                $domaincount++;
                $domainarray[$domaincount] = $DOMAIN_NAME;
                echo "$domaincount ";
                }
        $count2++;
    }
}
?>
What i would like to do now is save is the array domainarray into a text document, which contains all of the domain names which it needs, but i am not 100% certain of how to do that
Does anyone have any ideas?

Thank you ^.^

~Daniel

Re: Writing Array To File

Posted: Mon Aug 17, 2009 3:59 pm
by jackpf
fwrite() maybe? Or file_put_contents()?

Re: Writing Array To File

Posted: Mon Aug 17, 2009 4:06 pm
by Ollie Saunders
I need to know how to save an array into a .txt document as i cannot figure out how to do it.

Code: Select all

file_put_contents($filename, implode(PHP_EOL, $array));

Re: Writing Array To File

Posted: Tue Aug 18, 2009 10:09 am
by McInfo
To save an array to a file, first convert it to a string with serialize(). The array can be recovered with unserialize(). If you're asking about saving a list to a file, use Ollie's solution.

Edit: This post was recovered from search engine cache.

Re: Writing Array To File

Posted: Tue Aug 18, 2009 10:54 am
by jackpf
Yeah...you weren't very specific...