Page 1 of 1

fwrite not writing

Posted: Wed Oct 28, 2009 2:12 pm
by mehdihs
I have this code to output an xml file into a folder. The xml file gets created but its contents are blank. If I echo out the string that is being written into it I do see the string, but it refused to write into the file. The file itself is being created by the code but nothing is being written into the file. Could someone do a quick review and see what the issue is?

Code: Select all

 
<?php
header("Content-type: text/xml"); 
/*
echo "<?xml version='1.0' encoding='utf-8'?>" ;
*/
$xmlFile = "<?xml version='1.0' encoding='utf-8'?>";
 
//echo '<mainFolder>';
$xmlFile .= '<mainFolder>';
getDirectory('/Volumes/WEB/area/uploadtest');
 
function getDirectory( $path = "getcwd()")
{
    global $xmlFile;
    
    $ignore = array( 'cgi-bin', '.', '..','.svn','.DS_Store', 'folders.xml' );
    // Directories to ignore when listing output. 
 
    $dh = @opendir( $path );
    // Open the directory to the handle $dh
    
    while( false !== ( $file = readdir( $dh ) ) )
    {
    // Loop through the directory
        
        if( !in_array( $file, $ignore ) )
        {
            // Check that this file is not to be ignored
           
            
            if(is_dir( "$path/$file" ) )
            {
                // Its a directory, so we need to keep reading down...
                $folder = $file;
                //echo "<folder folderName='$folder'>";
                $xmlFile .= "<folder folderName='$folder'>";
                getDirectory( "$path/$file");
                //echo "</folder>";
                $xmlFile .= "</folder>";
                // Re-call this same function but on a new directory.
                // this is what makes function recursive.
            } 
            else 
            {
                $location = "$path/$file";
                $type = pathinfo($location, PATHINFO_EXTENSION);
                //echo "<file filename='$file' location = '$location'/>";
                $xmlFile .= "<file filename='$file' location = '$location' type='$type' />";
                // Just print out the filename
            }
        }
    }
    closedir( $dh );
    // Close the directory handle
}
//echo '</mainFolder>';
 
$xmlFile .= '</mainFolder>';
echo $xmlFile;
$xmlFileName = getcwd()."/folders.xml";
 
//echo $xmlFileName;
fopen($xmlFileName, "w+");
fwrite($xmlFileName,$xmlFile);
fclose($xmlFileName); 
?> 
 
 
As yu can see I am trying to write the variable $xmlFile into a text file called folders.xml. The folders.xml file does get created and if I echo out the $xmlFile it does echo out the string, so whats the issue?

Re: fwrite not writing

Posted: Wed Oct 28, 2009 3:33 pm
by requinix
Try

Code: Select all

error_reporting(65535);
ini_set("display_errors", true);
 
//echo $xmlFileName;
fopen($xmlFileName, "w+");
fwrite($xmlFileName,$xmlFile);
fclose($xmlFileName);
and see what happens.