// PROCESS XML CONTENT INTO DYNAMICALLY-NAMED ARRAYS
foreach (array('mime', 'state', 'country') as $val) {
$parser = xml_parser_create();
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($myXMLArray) - 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 $mimeImageRulesArray = 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("$basePath/xml/image_mime.xml", 'r');
print_r(filesize("$basePath/xml/image_mime.xml"));
if ($fileID) {
$xmlStuff = fread($fileID,
filesize("$basePath/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("$basePath/include/size.txt", 'r');
if ($fileID) {
$this->sizeTxt = fread($fileID,
filesize("$basePath/include/size.txt"));
fclose($fileID);
if (!is_numeric($this->sizeTxt)) $this->sizeTxt = '';
}
return $this->sizeTxt;
}
function setMimeXML() { // VOID METHOD TO SET/UPDATE image_mime.xml
WITH XML CONTENT
global $basePath;
$imageVideoMimeTypeArray = 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($mimeArray); $i++) {
list($mime, $ext) = explode("\t", $mimeArray[$i]);
$objArray = array();
$objArray[0] =& $this; $objArray[1] = 'array_search_bit';
$objArray[2] = $this->mimeImageRulesArray;
if ($this->isFoundInMimeImageRulesArray($mime))
array_push($imageVideoMimeTypeArray, $mime);
}
if (@sizeof($imageVideoMimeTypeArray) > 0) $xmlString =
$this->getMimeXML();
$xmlString = preg_replace('/[\n]+<\/mime_types>$/i', '',
$xmlString);
if (!$xmlString || strlen($xmlString) == 0)
$xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"
?>\n<mime_types>\n";
for ($i = 0; $i < @sizeof($imageVideoMimeTypeArray); $i++) {
$xmlString .= ' <mime id="' . ($i + 1) . '" name="' .
$this->xmlCleanup($imageVideoMimeTypeArray[$i]) .
"\"></mime>\n";
}
$xmlString .= '</mime_types>';
if (@sizeof($imageVideoMimeTypeArray) > 0) {
if (!is_dir("$basePath/xml")) mkdir("$basePath/xml", 0700);
$fileID = @fopen("$basePath/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("$basePath/include")) mkdir("$basePath/include", 0700);
$fileID = @fopen("$basePath/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("$basePath/include/size.txt", 0777); // MAKE IT UNIVERSAL
SINCE IT WILL ONLY CONTAIN AN UNCLEARLY ASSIGNED NUMBER
}
//-----------------------------------------* END OF GETTER/SETTER
METHODS *-----------------------------------------
function isFoundInMimeImageRulesArray($mime) {
list($mime, $stuff) = explode('/', $mime);
if (in_array($mime, $this->mimeImageRulesArray)) return true;
return false;
}
function isUnchangedMimeFile() { // BOOLEAN METHOD
global $basePath;
$sizeTxt = $this->getSizeTxt();
if ((is_numeric($sizeTxt) && $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->isUnchangedMimeFile()) {
if (!$this->sizeTxt) $this->setSizeTxt();
$this->setMimeXML();
}
}
// --- END OF PUBLIC FUNCTION process() ----------------------------
function xmlCleanup($text) { // STRING METHOD
$text = str_replace('"', '\\"', $text);
$text = str_replace("'", "\\'", $text);
$text = str_replace('&', '&', $text);
$text = str_replace('<', '<', $text);
$text = str_replace('>', '>', $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