Generate Multiples XML with PHP <p>I have a PHP script that is generating an XML file. He reads the contents of the subfolders in the folder X, and generates only one XML with all subfolders and their contents. I'm wanting the same script generates an XML for each subfolder to find it, would someone help me?</p> <pre><code>&lt;?php $sortorder = $_REQUEST['sortorder']; $indir = '../galleries'; $files = opendir($indir); $outxml = &lt;'xml version=\'1.0\' encoding=\'utf-8\'?&gt;\n\n'; $outxml .= '&lt;galleries&gt;\n\n'; while ($file = readdir($files)) { if(is_dir($indir.'/'.$file)){ if ($file != '.' &amp;&amp; $file != '..') { $tmp_outxml[] = $file; } } } if ($sortorder == 'date') { foreach ($tmp_outxml as $k =&gt; $v) { $modified = filemtime ($indir.'/'.$v); $moddate[$k] = $modified; } array_multisort ($moddate, $tmp_outxml); } else if ($sortorder == 'random') { shuffle ($tmp_outxml); } else { sort ($tmp_outxml); } foreach ($tmp_outxml as $v) { $outxml .= get_folder_xml($indir.'/'.$v,$v); } $outxml .= "&lt;/galleries&gt;"; closedir($files); $fp = fopen('../xml/gallery.xml', 'w'); fwrite($fp, $outxml); fclose($fp); function get_folder_xml($imageDir,$title) { $extensions = array('.jpg', '.jpeg', '.JPG','.JPEG','.gif','.GIF','.swf','.SWF'); $output = ''; if($folder = opendir($imageDir)) { $filenames=array(); while (false !== ($file = readdir($folder))) { $dot = strrchr($file, '.'); if(in_array($dot, $extensions)) { array_push($filenames, $file); } } $spaceTitle = str_replace('_',' ',$title); $newPath = str_replace('../','',$imageDir); $output .= '&lt;gallery&gt;\n'; $output .= '&lt;title&gt;$spaceTitle&lt;/title&gt;\n'; $output .= ' &lt;images&gt;\n'; foreach ($filenames as $source) { $imageName = str_replace('mainThumb_','',$source); $finalName = str_replace('_',' ',$imageName); $imageName = str_replace($extensions,'',$finalName); $output .= '\t &lt;image src=\'$newPath/$source\' thumb=\'$newPath/thumbs/$source\'&gt;$imageName&lt;/image&gt;\n'; } $output .= ' &lt;/images&gt;\n'; $output .= '&lt;/gallery&gt;\n\n'; return $output; closedir($folder); } } ?&gt; </code></pre> <p>i think i need to modify on this line, but i dont know how... </p> <pre><code>$fp = fopen('../xml/gallery.xml', 'w'); fwrite($fp, $outxml); fclose($fp); </code></pre> <p>ty for help!!</p>