首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未在/tmp目录中保存文件的PHPWord

未在/tmp目录中保存文件的PHPWord
EN

Stack Overflow用户
提问于 2020-12-27 21:24:01
回答 1查看 432关注 0票数 0

我正试着在我的Mac上用here运行PHPWord的基本示例。

我使用XAMPP作为应用服务器。

我只更改了require_once部分,并在文件名前面添加了sys_get_temp_dir(),因为"ZipArchive::close(): Failure to create temporary file: Permission denied“错误与this问题中的错误相同。

我的index.php文件如下:

代码语言:javascript
复制
<?php
require_once 'vendor/autoload.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)'
);

/*
 * Note: it's possible to customize font style of the Text element you add in three ways:
 * - inline;
 * - using named font style (new font style object will be implicitly created);
 * - using explicitly created font style object.
 */

// Adding Text element with font customized inline...
$section->addText(
    '"Great achievement is usually born of great sacrifice, '
        . 'and is never the result of selfishness." '
        . '(Napoleon Hill)',
    array('name' => 'Tahoma', 'size' => 10)
);

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
    $fontStyleName,
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
    '"The greatest accomplishment is not in never falling, '
        . 'but in rising again after you fall." '
        . '(Vince Lombardi)',
    $fontStyleName
);

// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);

echo(sys_get_temp_dir() . '/helloWorld.docx');
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save(sys_get_temp_dir() . '/helloWorld.docx');

/* Note: we skip RTF, because it's not XML-based and requires a different example. */
/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */

但是,它没有给出错误,也没有创建helloWorld.docx文件。原因是什么?我如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-27 21:45:01

(1)请尝试更改

代码语言:javascript
复制
$objWriter->save(sys_get_temp_dir() . '/helloWorld.docx');

代码语言:javascript
复制
$objWriter->save('./helloWorld.docx');

(2)确保您对当前文件夹有写入权限。

(3)运行程序,查看是否在当前文件夹中创建了helloWorld.docx

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65466230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档