【问题标题】:PHPWord not saving file in /tmp directoryPHPWord 未将文件保存在 /tmp 目录中
【发布时间】:2020-12-27 13:24:01
【问题描述】:

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

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

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

我的index.php文件如下:

<?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 文件。 是什么原因,我该如何解决这个问题?

【问题讨论】:

    标签: php phpword


    【解决方案1】:

    (1) 请尝试更改

    $objWriter->save(sys_get_temp_dir() . '/helloWorld.docx');
    

    到

    $objWriter->save('./helloWorld.docx');
    

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

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

    【讨论】:

    • 谢谢。只有当我将chmod 777 提供给 htdocs 时,我才能成功。还有其他“更好”的选择吗?
    • 很高兴知道您解决了问题。我想你也可以试试 chmod a+w 到 htdocs。 (仅授予写入权限)。另一种方法是创建一个子文件夹,说“outdoc”,以便您保存到 ./outdoc/helloWorld.docx 并使 outdoc 可写
    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多