AnsweredAssumed Answered

500: php_error_after_api when trying to download a file

Question asked by Francesca Shiekh on Feb 18, 2015
Latest reply on Dec 10, 2015 by kshah2411
I have a custom subpanel button on a custom documents-type module that calls and API which in turn zips all the files from that subpanel module related to the parent module and I want to download the zip.

The zip file is created in uploads/tmp and works well, the download of it not so well...
This is most likely due to a lack of understanding of the output of APIs and how it's structured.
I went through a number of APIs on SugarCRM v7.5.0.1, most require an existing bean or a file related to a module even if not to a bean for temporary files... all to no avail.
I keep coming back to the same problem:
The file is dumped in the sugarcrm log along with a PHP error.
The messagein the log is

A PHP error occured:
               

PW?JF7???#@%

 

            1_Shiekh.jpg͚eT?O??4Hh?æp ? I???q'Hw'8

 

??????????7???s???                                  ?N?????B??ƂӸ??????3??????y>ݒ?z?n?å?åØ+?+????????z?)'??????Ɗ???چ?????????

 

                  .???å?o8??@l, ????G@FH??O@I??????????????????????Å??????=??+1I^A!?:A???%?'? $?'?a?/?c???????@'A? Aæ?????B?????c'baqp???V?'??a'?cb'aab????0I?H?e??j??Ø?>å??aÅ??#FAI??????SHXDTL?囷r?

 

????ut?z??'????????????74,<"2*:&)9%5-=?+47/???????????????????'phxdj6G??ͯ!?76?????NN??/.??o??B'?????Ø$??1110??p??æ?S????TVh??I???/9?ݸ̂?? 

 

<much more of this garbage>

An exception happened: ( 500: php_error_after_api)Your request failed to complete.  A fatal error occurred.  Check logs for more details.

The same 500 error is displayed on the front end of the application on an ominous gray page.

Below is the definition of the api as you can see I tried using rawReply, and allowDownloadCookie, but I'm doing all these things pretty much blind because I don't understand how the output is or should be structured or what the effect of such entries is and can't find any documentation on them (maybe I'm looking the wrong place?).

I can successfully return the filename to the javascript that called the API if I don't even try to download the zip, but nothing more.

Any suggestions or pointers welcome.

Thanks,

FrancescaS


<?php
class zipFilesApi extends SugarApi
{
  public function registerApiRest() {
    return array(
      'zipFiles' => array(
        'reqType' => 'GET',
        'path' => array('<module>','zipFiles','relatedToModule','?','modelId','?','selection','?'),
        'pathVars' => array('module','','','related_module','','model_id','','selection'),
        'method' => 'zipFiles',
        //'rawReply' => true,
        //'allowDownloadCookie' => true,
        'shortHelp' => 'Custom WR. Zip files related to module/model_id. Use selection to specify all or list ids of files to zip',
        'longHelp' => '',
      ),
    );
  }
  function zipFiles($api, $args)
  { <create the ZipArchive, make sure it can be created etc.>
        $zip->close();
        if(file_exists($zip_path)){
          $GLOBALS['log']->fatal('Created:: ' .$zip_path);
          $filepath = $zip_path;
          $filesize = filesize($filepath);
          $display_filename = basename($filepath);
          header("Pragmai: public");
          header("Cache-Control: maxage=1, post-check=0, pre-check=0");
          header("X-Content-Type-Options: nosniff");
          header("Content-Type: application/force-download");
          header("Content-Type: application/zip");
          header("Content-Disposition: attachment; filename=\"".$display_filename."\"");
          header("Content-Lenght: " .$filesize);
          header("Expires:" . TimeDate::httpTime(time() + 2592000));
          set_time_limit(0);
          ob_end_clean();
          ob_start();
          readfile($filepath);
          ob_end_flush();
          register_shutdown_function(
            function () use($filepath) {
               if (is_file($filepath)) {
                 unlink($filepath);
               }
            }
          );
          return; //should I be returning something?
        }else{
          throw new SugarApiException('Error Creating Zip File');
        }
      }else{
        throw new SugarApiException('Zip file not found');
      }
    }
  }
}
?>                                                                       

Outcomes