codeigniter phpword模板处理器返回值?

qncylg1j  于 2023-03-06  发布在  PHP
关注(0)|答案(1)|浏览(116)

$template_processor->saveAs()的返回值是什么?,我尝试捕获返回值以确定进程是否成功执行

public function create_spm($data_spm){
    $templateFile = FCPATH.'upload_file/template_spm/template_spm.docx';
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $template_file = $templateFile;
    $template_processor = new \PhpOffice\PhpWord\TemplateProcessor($template_file);
    $template_processor->setValues($data_spm);
    $output_file = FCPATH.'upload_file/spm/spm.docx';
    $hasil=$template_processor->saveAs($output_file);
    var_dump($hasil);
    exit();
}

返回值始终为NULL
我尝试捕获$template_processor-〉saveAs()的返回值,但它总是返回NULL

6kkfgxo0

6kkfgxo01#

得到NULL是因为函数没有返回基于源代码的任何内容:

/**
 * Saves the result document to the user defined file.
 *
 * @since 0.8.0
 *
 * @param string $fileName
 */
public function saveAs($fileName): void
{
    $tempFileName = $this->save();

    if (file_exists($fileName)) {
        unlink($fileName);
    }

    /*
     * Note: we do not use `rename` function here, because it loses file ownership data on Windows platform.
     * As a result, user cannot open the file directly getting "Access denied" message.
     *
     * @see https://github.com/PHPOffice/PHPWord/issues/532
     */
    copy($tempFileName, $fileName);
    unlink($tempFileName);
}

为了成功执行检查,我使用file_exists($output_file)检查saveAS($output_file)之后的文件是否存在
希望这能有所帮助,快乐编码!

相关问题