[thelist] writing array contents to file

Aaron Vegh aaronvegh at gmail.com
Mon Jun 23 13:42:19 CDT 2008


I've got a working solution, in case anyone else ever needs to do this. :-)

arrayToDir($array, array($dir, "library")); // second argument is the
base directory you want to build from.

function arrayToDir($package, $paths=array()) {
       {

		$i = 0;
		foreach ($package as $key=>$value)
		{
			$path = '';
			foreach ($paths as $p)
			{
				$path .= (($path == '') ? $p : "/$p");
			}
			if (strpos($key, ".") == false)
			{
				$paths[] = $key;
				addDirectory((($path == '') ? "$key/" : "$path/$key/"));
				arrayToDir($package[$key], &$paths);
			}
			else
			{
				addFile($value, (($path == '') ? $key : "$path/$key"));
			}
			if ($i == (sizeof($package)-1))
			{
				array_pop($paths);
			}
			$i++;
		}
	}
}

function addDirectory($path) {
	mkdir($path);
}

function addFile($data, $path) {
	file_put_contents($path, $data);
}


Cheers!
Aaron.

On Sun, Jun 22, 2008 at 11:07 PM, Aaron Vegh <aaronvegh at gmail.com> wrote:
> Hi Jeffrey,
> Thanks very much for your effort.
>
>> The
>> following code works for your sample array, but it has serious limitations:
>
> Unfortunately, my simplified array appears to have been too simple.
> :-P Your code doesn't work on the real array. Actually right now it's
> giving me permission errors when it hits the mkdir() call, which is
> odd because all the directories are being given full read/write
> privileges (and I've confirmed it on the filesystem). I'm going to
> continue to investigate; let me know if that twigs anything for you!
>
> Cheers,
> Aaron.
>
>>
>> 1. If you reverse the order of the 'library' array, all of the directories
>> get created, but the structure is broken.
>>
>> 2. If you add a third level of subdirectories, the structure gets broken.
>>
>> Hopefully this helps,
>> Jeffrey
>>
>> <?php
>>
>> $dirRoot = $_SERVER['DOCUMENT_ROOT'] . '/';
>> $curPath = '';
>> $depth = 0;
>>
>> $array = array(
>>        'setup' => array (
>>                'file1.txt'=>'file1 contents',
>>                'file2.txt'=>'file2 contents'
>>        ),
>>        'images' => array (
>>                'image1.gif'=>'image1contents',
>>                'image2.gif'=>'image2contents'
>>        ),
>>        'library' => array(
>>                'settings_conf' => array (
>>                        'conf1.txt'=>'conf1 contents',
>>                        'conf2.txt'=>'conf2 contents'
>>                ),
>>                'settings_conf1.txt'=>'contents'
>>        ),
>>        'config.txt'=>'configuration contents'
>> );
>>
>> function arrayToDir($array, $curPath = '') {
>>        global $dirRoot, $curPath, $depth;
>>        foreach ($array as $key => $val) {\
>>                if (strpos($key, '.') === false) {
>>                        mkdir($dirRoot . $curPath . $key, 0777);
>>                }
>>                if (is_array($val)) {
>>                        if ($depth == 0) {
>>                                $curPath = $key . '/';
>>                                $depth++;
>>                        }
>>                        arrayToDir($val, $curPath);
>>                } else {
>>                        if ($depth > 0) {
>>                                $depth--;
>>                        }
>>                        if ($depth == 0) {
>>                                $curPath = '';
>>                        }
>>                }
>>
>>        }
>> }
>>
>> arrayToDir($array);
>> echo '<p>Done!</p>';
>>
>> ?>
>>
>
>
>
> --
> Aaron Vegh, Principal
> Innoveghtive Inc.
> P: (647) 477-2690
> C: (905) 924-1220
> www.innoveghtive.com
> www.website-in-a-day.com
>



-- 
Aaron Vegh, Principal
Innoveghtive Inc.
P: (647) 477-2690
C: (905) 924-1220
www.innoveghtive.com
www.website-in-a-day.com



More information about the thelist mailing list