Categories > OpenTBS with DOCX >

Allowed memory size exhausted FATAL ERROR

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Raul
Date: 2013-02-25
Time: 17:09

Allowed memory size exhausted FATAL ERROR

My code is giving an error, which I do not understand why.

Here is the error message:
Fatal error: Allowed memory size of 18874368 bytes exhausted (tried to allocate 7575066 bytes) in /.../tbs_class.php on line 2223

By: Sarah
Date: 2013-02-25
Time: 17:16

Re: Allowed memory size exhausted FATAL ERROR

It looks like you have an infinite loop. Can we see your code?
By: Raul
Date: 2013-02-25
Time: 17:50

Re: Allowed memory size exhausted FATAL ERROR

There is no infinite loop. The code perform a mail merge using a MS Word .docx template.

Here is the code:
<?php

$conn = @mysql_pconnect("", "", "");
$selectedDB = @mysql_select_db("");

if(!$conn || !$selectedDB){
    echo "No se puede conectar con la base de datos....";
    exit();
}

//Hacer la busqueda de miembros con FVL por vencerse.
$date = new DateTime(date("Y-m-d"));
$date->modify('+180 day');
$sixMonthLater = $date->format('Y-m-d');

$dateDelimiter = new DateTime(date("Y-m-d"));
$dateDelimiter->modify('-6 month');
$delimiterDate = $dateDelimiter->format('Y-m-d');


$q = "QUERY SENTENCE...";



// load the TinyButStrong libraries
include_once('tbs_class.php');

// load the OpenTBS plugin
if (file_exists('tbs_plugin_opentbs.php')) {
    include_once('tbs_plugin_opentbs.php');
} else {
    echo "No se encuentra el plug-in.";exit();
}

$TBS = new clsTinyButStrong; // new instance of TBS
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load OpenTBS plugin

// Read user choices
if (!isset($_POST['btnSubmit'])) exit("No se puede procesar. Debe acceder a traves de prueba.php");
$suffix = (isset($_POST['suffix']) && (trim($_POST['suffix'])!=='') && ($_SERVER['SERVER_NAME']=='localhost')) ? trim($_POST['suffix']) : '';

$expiredBeforeToday = array();
$expiredToday = array();
$thirtyToExpire = array();
$ninetyToExpire = array();
$sixToExpire = array();

$ninetyDays = new DateTime(date("Y-m-d"));
$ninetyDays->modify('+90 day');

$thirtyDays = new DateTime(date("Y-m-d"));
$thirtyDays->modify('+30 day');

$todayDate = new DateTime(date("Y-m-d"));

$query = @mysql_query($q);
$hoy = date("Y-m-d");
while($row = mysql_fetch_assoc($query)){
    $fvl = new DateTime($row['fvl']);
    if($fvl < $todayDate){
        $expiredBeforeToday[] = array('num_miembro'=>$row['num_miembro'], 'fecha'=>formatDate(substr($hoy, 8, 2), substr($hoy, 5, 2), substr($hoy, 0, 4)), 'nombre'=>(empty($row['ini']) ? $row['nombre'] : $row['nombre']." ".$row['ini']), 'paterno'=>$row['paterno'], 'materno'=>$row['materno'], 'user'=>$row['user'], 'password'=>$row['password'], 'dir1'=>(empty($row['dir2']) ? $row['dir1'] : $row['dir1']."\r\n".$row['dir2']), 'pueblo'=>$row['pueblo'], 'pais'=>$row['pais'], 'zipcode'=>(empty($row['zip4']) ? $row['zipcode'] : $row['zipcode']."-".$row['zip4']));
    }
    else if($fvl == $todayDate){
        $expiredToday[] = array('num_miembro'=>$row['num_miembro'], 'fecha'=>formatDate(substr($hoy, 8, 2), substr($hoy, 5, 2), substr($hoy, 0, 4)), 'nombre'=>(empty($row['ini']) ? $row['nombre'] : $row['nombre']." ".$row['ini']), 'paterno'=>$row['paterno'], 'materno'=>$row['materno'], 'user'=>$row['user'], 'password'=>$row['password'], 'dir1'=>(empty($row['dir2']) ? $row['dir1'] : $row['dir1']."\r\n".$row['dir2']), 'pueblo'=>$row['pueblo'], 'pais'=>$row['pais'], 'zipcode'=>(empty($row['zip4']) ? $row['zipcode'] : $row['zipcode']."-".$row['zip4']));
    }
    else if($todayDate < $fvl && $fvl < $thirtyDays){
        $thirtyToExpire[] = array('num_miembro'=>$row['num_miembro'], 'fecha'=>formatDate(substr($hoy, 8, 2), substr($hoy, 5, 2), substr($hoy, 0, 4)), 'nombre'=>(empty($row['ini']) ? $row['nombre'] : $row['nombre']." ".$row['ini']), 'paterno'=>$row['paterno'], 'materno'=>$row['materno'], 'user'=>$row['user'], 'password'=>$row['password'], 'dir1'=>(empty($row['dir2']) ? $row['dir1'] : $row['dir1']."\r\n".$row['dir2']), 'pueblo'=>$row['pueblo'], 'pais'=>$row['pais'], 'zipcode'=>(empty($row['zip4']) ? $row['zipcode'] : $row['zipcode']."-".$row['zip4']));
    }
    else if($thirtyDays < $fvl && $fvl < $ninetyDays){
        $ninetyToExpire[] = array('num_miembro'=>$row['num_miembro'], 'fecha'=>formatDate(substr($hoy, 8, 2), substr($hoy, 5, 2), substr($hoy, 0, 4)), 'nombre'=>(empty($row['ini']) ? $row['nombre'] : $row['nombre']." ".$row['ini']), 'paterno'=>$row['paterno'], 'materno'=>$row['materno'], 'user'=>$row['user'], 'password'=>$row['password'], 'dir1'=>(empty($row['dir2']) ? $row['dir1'] : $row['dir1']."\r\n".$row['dir2']), 'pueblo'=>$row['pueblo'], 'pais'=>$row['pais'], 'zipcode'=>(empty($row['zip4']) ? $row['zipcode'] : $row['zipcode']."-".$row['zip4']));
    }
    else{
        $sixToExpire[] = array('num_miembro'=>$row['num_miembro'], 'fecha'=>formatDate(substr($hoy, 8, 2), substr($hoy, 5, 2), substr($hoy, 0, 4)), 'nombre'=>(empty($row['ini']) ? $row['nombre'] : $row['nombre']." ".$row['ini']), 'paterno'=>$row['paterno'], 'materno'=>$row['materno'], 'user'=>$row['user'], 'password'=>$row['password'], 'dir1'=>(empty($row['dir2']) ? $row['dir1'] : $row['dir1']."\r\n".$row['dir2']), 'pueblo'=>$row['pueblo'], 'pais'=>$row['pais'], 'zipcode'=>(empty($row['zip4']) ? $row['zipcode'] : $row['zipcode']."-".$row['zip4']));
    }
   
    unset($fvl);
}

if(!empty($expiredBeforeToday)){
    $template = basename("pruebaExpirado_antes.docx");
    $x = pathinfo($template);
    $template_ext = $x['extension'];
   
    if (file_exists($template)){
   
    $TBS->LoadTemplate($template);
   
    // Merge data
    $TBS->MergeBlock('b', $expiredBeforeToday);
   
    // delete comments
    $TBS->PlugIn(OPENTBS_DELETE_COMMENTS);
   
    // Define the name of the output file
    $file_name = "afvl_expirado_antes.docx";
   
    if ($suffix==='') {
        // Save
        $TBS->Show(OPENTBS_FILE, $file_name);
    } else {
        // save as file
        $file_name = str_replace('.','_'.$suffix.'.',$file_name);
        $TBS->Show(OPENTBS_FILE+TBS_EXIT, $file_name);
    }
   
    }
}

if(!empty($expiredToday)){
    $template = basename("pruebaExpirado_hoy.docx");
    $x = pathinfo($template);
    $template_ext = $x['extension'];
   
    if (file_exists($template)){
   
    $TBS->LoadTemplate($template);
   
    // Merge data
    $TBS->MergeBlock('b', $expiredToday);
   
    // delete comments
    $TBS->PlugIn(OPENTBS_DELETE_COMMENTS);
   
    // Define the name of the output file
    $file_name = "afvl_expirado_hoy.docx";
   
    if ($suffix==='') {
        // Save
        $TBS->Show(OPENTBS_FILE, $file_name);
    } else {
        // save as file
        $file_name = str_replace('.','_'.$suffix.'.',$file_name);
        $TBS->Show(OPENTBS_FILE+TBS_EXIT, $file_name);
    }
   
    }
}

if(!empty($thirtyToExpire)){
    $template = basename("pruebaExpirado_treinta.docx");
    $x = pathinfo($template);
    $template_ext = $x['extension'];
   
    if (file_exists($template)){
   
    $TBS->LoadTemplate($template);
   
    // Merge data
    $TBS->MergeBlock('b', $thirtyToExpire);
   
    // delete comments
    $TBS->PlugIn(OPENTBS_DELETE_COMMENTS);
   
    // Define the name of the output file
    $file_name = "afvl_treinta_para_expirar.docx";
   
    if ($suffix==='') {
        // Save
        $TBS->Show(OPENTBS_FILE, $file_name);
    } else {
        // save as file
        $file_name = str_replace('.','_'.$suffix.'.',$file_name);
        $TBS->Show(OPENTBS_FILE+TBS_EXIT, $file_name);
    }
   
    }
}

if(!empty($ninetyToExpire)){
    $template = basename("pruebaExpirado_noventa.docx");
    $x = pathinfo($template);
    $template_ext = $x['extension'];
   
    if (file_exists($template)){
   
    $TBS->LoadTemplate($template);
   
    // Merge data
    $TBS->MergeBlock('b', $ninetyToExpire);
   
    // delete comments
    $TBS->PlugIn(OPENTBS_DELETE_COMMENTS);
   
    // Define the name of the output file
    $file_name = "afvl_noventa_para_expirar.docx";
   
    if ($suffix==='') {
        // Save
        $TBS->Show(OPENTBS_FILE, $file_name);
    } else {
        // save as file
        $file_name = str_replace('.','_'.$suffix.'.',$file_name);
        $TBS->Show(OPENTBS_FILE+TBS_EXIT, $file_name);
    }
   
    }
}

if(!empty($sixToExpire)){
    $template = basename("carta_renovacion.docx");
    $x = pathinfo($template);
    $template_ext = $x['extension'];
   
    if (file_exists($template)){
   
    $TBS->LoadTemplate($template);
   
    // Merge data
    $TBS->MergeBlock('b', $sixToExpire);
   
    // delete comments
    $TBS->PlugIn(OPENTBS_DELETE_COMMENTS);
   
    // Define the name of the output file
    $file_name = "afvl_seis_para_expirar.docx";
   
    if ($suffix==='') {
        // Save
        $TBS->Show(OPENTBS_FILE, $file_name);
    } else {
        // save as file
        $file_name = str_replace('.','_'.$suffix.'.',$file_name);
        $TBS->Show(OPENTBS_FILE+TBS_EXIT, $file_name);
    }
   
    }
}

exit();

?>


<?php

function formatDate($day, $month, $year){
    switch($month){
        case "01":
            $month = "enero";
            break;
        case "02":
            $month = "febrero";
            break;
        case "03":
            $month = "marzo";
            break;
        case "04":
            $month = "abril";
            break;
        case "05":
            $month = "mayo";
            break;
        case "06":
            $month = "junio";
            break;
        case "07":
            $month = "julio";
            break;
        case "08":
            $month = "agosto";
            break;
        case "09":
            $month = "septiembre";
            break;
        case 10:
            $month = "octubre";
            break;
        case 11:
            $month = "noviembre";
            break;
        case 12:
            $month = "diciembre";
            break;
    }
   
    return $day." de ".$month." de ".$year;
   
}

?>
By: Skrol29
Date: 2013-02-26
Time: 11:53

Re: Allowed memory size exhausted FATAL ERROR

Hi Paul,

Thanks for your code.

The memory error can be due to a very big amount of data on a MergeBlock(), or maybe because you're using only one TBS instance for merging several documents.

I suggest that you first try re-instaciate TBS before each new document :

$TBS = new clsTinyButStrong; // new instance of TBS
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load OpenTBS plugin
$TBS->LoadTemplate($template);

If the error still raises, then try to find which MergeBlock() makes the error and see if the amount of data is very big or not.
If its' very big, then it is legitimate to increase the value of the memory_limit directive in the PHP ini file.

If none of this solutions is possible, then there is a complicated solution but it needs to extract the sub-file from the DOCX archive, then merge it using plug-on OnFly, and then put the file back to the archive.
By: Anonymous
Date: 2013-02-26
Time: 18:36

Re: Allowed memory size exhausted FATAL ERROR

Hi Skrol29,

Increasing the memory_limit in the php.ini file worked for me. I feel silly for not having thought of that before. Lol

Anyways, you saved me a lot of trouble,

Thanks!!!