You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac系统XAMPP环境下Codeigniter项目PHPWord导出权限问题求助

Codeigniter+PHPWord在Mac XAMPP环境下导出Word的权限问题解决方案

问题背景

在Mac系统的XAMPP环境中开发Codeigniter项目,使用PHPWord导出Word文档时持续遇到权限错误。尝试修改项目目录所有者、权限等方法均无效。

导出代码

public function saveWord()
{
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();
    $section->addText('Hello World!');
    $file = 'HelloWorld.docx';
    // header("Content-Description: File Transfer");
    // header('Content-Disposition: attachment; filename="' . $file . '"');
    // header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    // header('Content-Transfer-Encoding: binary');
    // header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    // header('Expires: 0');
    $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $xmlWriter->save("php://output");
}

错误信息

  • Message: mkdir(): Permission denied
    Filename: Writer/AbstractWriter.php
  • Message: tempnam(): file created in the system's temporary directory
    Filename: Writer/AbstractWriter.php
  • Message: ZipArchive::close(): Failure to create temporary file: No such file or directory
    Filename: Shared/ZipArchive.php
  • Type: PhpOffice\PhpWord\Exception\Exception
    Message: Could not close zip file php://output:
    Filename: /Applications/XAMPP/xamppfiles/htdocs/kvkkolay/vendor/phpoffice/phpword/src/PhpWord/Shared/ZipArchive.php

已尝试操作

  • 使用chown -R myusername:mygroup path/to/folder修改项目目录所有者
  • 使用chmod -R 0777 path/to/folder修改目录权限
  • 通过whoami确认PHP运行用户为daemon
  • 发现PHPWord尝试在/var/folders/45/zwgpkpmx3kj0hypypctn67rh0000gn/T/PHPWordWriter_635a3ef3f02cf5.54543358创建临时文件夹,但无法修改该系统临时目录权限

解决方案

方案1:指定PHPWord临时目录到项目内可写路径

系统临时目录对daemon用户无写入权限,可手动设置PHPWord使用项目内的临时文件夹:

  1. 创建项目临时目录并设置权限
    在终端执行以下命令:

    mkdir /Applications/XAMPP/xamppfiles/htdocs/kvkkolay/temp
    chown daemon:daemon /Applications/XAMPP/xamppfiles/htdocs/kvkkolay/temp
    chmod 755 /Applications/XAMPP/xamppfiles/htdocs/kvkkolay/temp
    
  2. 修改导出代码添加临时目录配置

    public function saveWord()
    {
        // 设置PHPWord临时目录为项目内的temp文件夹
        \PhpOffice\PhpWord\Settings::setTempDir(FCPATH . 'temp');
        
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $section = $phpWord->addSection();
        $section->addText('Hello World!');
        $file = 'HelloWorld.docx';
        
        // 恢复header配置以触发文件下载
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $file . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');
        
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $xmlWriter->save("php://output");
    }
    

    注:FCPATH是Codeigniter内置常量,指向项目根目录。

方案2:修改XAMPP的PHP临时目录配置

通过调整PHP的系统临时目录配置,让daemon用户拥有写入权限:

  1. 编辑php.ini文件
    打开/Applications/XAMPP/xamppfiles/etc/php.ini,找到以下配置项并修改:

    upload_tmp_dir = "/Applications/XAMPP/xamppfiles/temp"
    sys_temp_dir = "/Applications/XAMPP/xamppfiles/temp"
    
  2. 创建目录并设置权限
    在终端执行:

    mkdir /Applications/XAMPP/xamppfiles/temp
    chown daemon:daemon /Applications/XAMPP/xamppfiles/temp
    chmod 755 /Applications/XAMPP/xamppfiles/temp
    
  3. 重启XAMPP Apache服务
    在XAMPP控制面板中重启Apache,使配置生效。

方案3:先保存到本地再输出到浏览器

若以上方案无效,可先将Word文件保存到项目内可写目录,再读取输出给浏览器:

public function saveWord()
{
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();
    $section->addText('Hello World!');
    $localFile = FCPATH . 'temp/HelloWorld.docx';
    $downloadFile = 'HelloWorld.docx';
    
    $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $xmlWriter->save($localFile);
    
    // 输出文件到浏览器
    header("Content-Description: File Transfer");
    header('Content-Disposition: attachment; filename="' . $downloadFile . '"');
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Content-Length: ' . filesize($localFile));
    
    readfile($localFile);
    // 可选:删除临时文件
    unlink($localFile);
}

确保temp文件夹权限已正确设置为daemon可写。


内容的提问来源于stack exchange,提问作者Çağatay Kaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 15:40:28