如何删除临时下载的文件?

时间:2016-07-12 08:20:20

标签: browser tcpdf temporary-files

当用户点击按钮时,我的网络应用程序通过tcpdf打开了pdf:

function showOverviewThroughPdf(){

    $oResp = $this->getResponse("tcpdf") ;

    //NEW PDF DOCUMENT
    $oResp->outputFileName = "liste_concepteur_matiere.pdf";
    $oResp->doDownload = false;
    $oResp->initPdf('L', 'mm', 'A4', null,  null);

    //settting
    $oResp->tcpdf->SetFont ('pdfahelvetica', '', 12) ;

    // set margins
    $oResp->tcpdf->SetMargins(20, 20, 20) ;
    $oResp->tcpdf->SetHeaderMargin(15) ;
    $oResp->tcpdf->SetFooterMargin(15) ;

    $oResp->tcpdf->SetAutoPageBreak(TRUE, 20) ;

    // set image scale factor
    $oResp->tcpdf->setImageScale(1.25);

    $oResp->tcpdf->AddPage();

    $iIdMatiere =  $_SESSION["id_matiere"] ;

    jClasses::inc ("sujet~questionSrv") ;
    jClasses::inc ("commun~toolsSrv") ;

    $oCurrentUser = jAuth::getUserSession() ;
    $oCurrentUser = toolsSrv::getByCondition ("utilisateurs~users", "login", $oCurrentUser->login) ;
    $oCurrentUser = $oCurrentUser [0] ;  

    $Questions = questionSrv::getAllQuestionsNotValidateByCondition ($iIdMatiere, null , $oCurrentUser->id, "id_question" , null,null, null) ;

    $zHtml = "";

    $i = 1 ;
    foreach ($Questions["toQuestions"] as $tab){
        if( $tab->id_type_question == 2){
             $zHtml .= "<dt> ". $i ." - " . questionSrv::text_to_texthtml($tab->lib_question)  . " </dt>" ;

             $zHtml .= "<dd>&nbsp;a)&nbsp;".    questionSrv::text_to_texthtml($tab->qcm_r1)."</dd>";    
             $zHtml .= "<dd>&nbsp;b)&nbsp;". questionSrv::text_to_texthtml($tab->qcm_r2)."</dd>";   
             $zHtml .= "<dd>&nbsp;c)&nbsp;". questionSrv::text_to_texthtml($tab->qcm_r3)."</dd>";    

            if(trim($tab->qcm_r4) !="")
            $zHtml .= "<dd>&nbsp;d)&nbsp;".questionSrv::text_to_texthtml($tab->qcm_r4)."</dd>"; 

            if(trim($tab->qcm_r5) !="") 
            $zHtml .= "<dd>&nbsp;e)&nbsp;".questionSrv::text_to_texthtml($tab->qcm_r5)."</dd>"; 

            if(trim($tab->qcm_r6) !="")
            $zHtml .= "<dd>&nbsp;f)&nbsp;".questionSrv::text_to_texthtml($tab->qcm_r6)."</dd>"  ; 

        } else {
            $zHtml .= "<dt>". $i ." - " . questionSrv::text_to_texthtml($tab->lib_question)  . "</dt>";

            if($tab->epreuve_phase_sess != 1){
                $zHtml .= "<dd>&nbsp;a)&nbsp;VRAI</dd>";    
                $zHtml .= "<dd>&nbsp;b)&nbsp;FAUX</dd>";    
            }
        }

        $i++;
    }

    $oResp->tcpdf->writeHTML($zHtml, true, 0, true, 0); // this opens the pdf thus creating a temporary file

    return $oResp ;
}

在运行时,客户端计算机中的浏览器会创建一个临时文件。那么如何在特定事件中删除此文件,例如当用户与我的应用程序断开连接时?

1 个答案:

答案 0 :(得分:0)

你需要返回pdf文件;只需在末尾添加这两行而不是返回$ oResp;

$oResp->tcpdf->Output($file_name, 'F');
return $file_name ;