PHP: filesize of XML file increases exponentially - HELP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    PHP: filesize of XML file increases exponentially - HELP

    // PROCESS XML CONTENT INTO DYNAMICALLY-NAMED ARRAYS
    foreach (array('mime', 'state', 'country') as $val) {
    $parser = xml_parser_crea te();
    xml_parser_set_ option($parser, XML_OPTION_SKIP _WHITE, 1);
    xml_parse_into_ struct($parser, ${$val . 'XML'}, ${$val .
    'XMLArray'}, $tags);
    xml_parser_free ($parser);
    $myXMLArray = ${$val . 'XMLArray'};
    for ($i = 1; $i < @sizeof($myXMLA rray) - 1; $i++) {
    if ($myXMLArray[$i]['attributes']['ABBREV']) {
    $this->{$val . 'Array'}['' .
    $myXMLArray[$i]['attributes']['ABBREV']] = $myXMLArray[$i]['value'];
    } else {
    $this->mimeArray['' . $myXMLArray[$i]['attributes']['ID']] =
    $myXMLArray[$i]['attributes']['NAME'];
    }
    }
    }


    The following code snipped dynamically reads XML content and parses
    into simple 3-dim arrays using xml_parse_into_ struct() command.
    However, upon running this snippet I've noticed the data moving
    extremely slowly, and then all of a sudden I get a Fatal Error that
    states that the allocated memory (I can't reproduce the exact error
    again I'm sorry) is full when attempting to allocate 3 bytes. The
    error occurred on the xml_parse_into_ struct() line. Following is the
    size of the XML files in question:

    state.xml - 2521 bytes
    country.xml - 12686 bytes

    However, upon further inspection I am noticing that I somehow am
    increasing the size of /tmp/xml/image_mime.xml exponentially and I
    have no idea how I'm doing it!

    Current filesize of /tmp/xml/image_mime.xml: 513133515335 bytes!!

    Here is the code that will check for the filesize of /etc/mime.types
    and if IT changes then change the contents of /tmp/xml/image_mime.xml
    to reflect that:


    class MIME_TO_XML {

    var $sizeTxt;
    var $mimeImageRules Array = array('image', 'video');

    function MIME_TO_MXL() { // CONSTRUCTOR
    return true;
    }

    //-----------------------------------------* GETTER/SETTER METHODS
    *-----------------------------------------


    function getMimeXML() { // STRING METHOD CONTAINING XML FILE
    CONTENTS
    global $basePath;
    $fileID = @fopen("$basePa th/xml/image_mime.xml" , 'r');
    print_r(filesiz e("$basePath/xml/image_mime.xml" ));
    if ($fileID) {
    $xmlStuff = fread($fileID,
    filesize("$base Path/xml/image_mime.xml" ));
    fclose($fileID) ;
    }
    return $xmlStuff;
    }

    function getSizeTxt() { // INTEGER METHOD RETURNS SIZE OF
    /etc/mime.types STORED IN size.txt OR RETURNS NULL
    global $basePath;
    $fileID = @fopen("$basePa th/include/size.txt", 'r');
    if ($fileID) {
    $this->sizeTxt = fread($fileID,
    filesize("$base Path/include/size.txt"));
    fclose($fileID) ;
    if (!is_numeric($t his->sizeTxt)) $this->sizeTxt = '';
    }
    return $this->sizeTxt;
    }


    function setMimeXML() { // VOID METHOD TO SET/UPDATE image_mime.xml
    WITH XML CONTENT
    global $basePath;
    $imageVideoMime TypeArray = array();
    $fileID = @fopen('/etc/mime.types', 'r') or die('Could not open mime
    types file');
    $mimeStuff = fread($fileID, filesize('/etc/mime.types'));
    fclose($fileID) ;
    $mimeArray = explode("\n", $mimeStuff);
    for ($i = 0; $i < @sizeof($mimeAr ray); $i++) {
    list($mime, $ext) = explode("\t", $mimeArray[$i]);
    $objArray = array();
    $objArray[0] =& $this; $objArray[1] = 'array_search_b it';
    $objArray[2] = $this->mimeImageRules Array;
    if ($this->isFoundInMimeI mageRulesArray( $mime))
    array_push($ima geVideoMimeType Array, $mime);
    }
    if (@sizeof($image VideoMimeTypeAr ray) > 0) $xmlString =
    $this->getMimeXML() ;
    $xmlString = preg_replace('/[\n]+<\/mime_types>$/i', '',
    $xmlString);
    if (!$xmlString || strlen($xmlStri ng) == 0)
    $xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"
    ?>\n<mime_types >\n";
    for ($i = 0; $i < @sizeof($imageV ideoMimeTypeArr ay); $i++) {
    $xmlString .= ' <mime id="' . ($i + 1) . '" name="' .
    $this->xmlCleanup($im ageVideoMimeTyp eArray[$i]) .
    "\"></mime>\n";
    }
    $xmlString .= '</mime_types>';
    if (@sizeof($image VideoMimeTypeAr ray) > 0) {
    if (!is_dir("$base Path/xml")) mkdir("$basePat h/xml", 0700);
    $fileID = @fopen("$basePa th/xml/image_mime.xml" , 'w') or die("Could
    not open $basePath/xml/image_mime.xml ");
    fputs($fileID, $xmlString); fflush($fileID) ; fclose($fileID) ;
    }

    }

    function setSizeTxt() { // VOID METHOD SETS size.txt WITH FILESIZE
    OF /etc/mime.types
    global $basePath;
    if (!is_dir("$base Path/include")) mkdir("$basePat h/include", 0700);
    $fileID = @fopen("$basePa th/include/size.txt", 'w') or die("Could
    not open $basepath/include/size.txt ");
    $this->sizeTxt = filesize('/etc/mime.types');
    fputs($fileID, $this->sizeTxt); fflush($fileID) ; fclose($fileID) ;
    chmod("$basePat h/include/size.txt", 0777); // MAKE IT UNIVERSAL
    SINCE IT WILL ONLY CONTAIN AN UNCLEARLY ASSIGNED NUMBER
    }


    //-----------------------------------------* END OF GETTER/SETTER
    METHODS *-----------------------------------------


    function isFoundInMimeIm ageRulesArray($ mime) {
    list($mime, $stuff) = explode('/', $mime);
    if (in_array($mime , $this->mimeImageRules Array)) return true;
    return false;
    }


    function isUnchangedMime File() { // BOOLEAN METHOD
    global $basePath;
    $sizeTxt = $this->getSizeTxt() ;
    if ((is_numeric($s izeTxt) && $sizeTxt !==
    filesize('/etc/mime.types')) || !$sizeTxt) return false;
    return true;
    }



    // --* THIS WILL BE THE ONLY METHOD YOU WILL HAVE TO RUN OUTSIDE *--

    function process() { // VOID METHOD - RUNS ALL OTHER METHODS
    if (!$this->isUnchangedMim eFile()) {
    if (!$this->sizeTxt) $this->setSizeTxt() ;
    $this->setMimeXML() ;
    }
    }

    // --- END OF PUBLIC FUNCTION process() ----------------------------





    function xmlCleanup($tex t) { // STRING METHOD
    $text = str_replace('"' , '\\"', $text);
    $text = str_replace("'" , "\\'", $text);
    $text = str_replace('&' , '&amp;', $text);
    $text = str_replace('<' , '&lt;', $text);
    $text = str_replace('>' , '&gt;', $text);
    $text = str_replace('=' , '&eq;', $text);
    return $text;
    }

    }


    Any ideas as to how to approach this problem? Ultimate goal is
    probably more simplistic than my approach (no surprise, I can't
    approach problems simplistically to save my life: Create an entity
    collection of image and/or video MIME types derived from
    /etc/mime.types which you should ideally create ONE TIME ONLY.

    Phil
Working...