我想提供一个 Excel 文件以供使用 PhpSpreadsheet 下载

这是我的代码:

    require 'vendor/autoload.php';

    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'Hello World !');

    $writer = new Xlsx($spreadsheet);
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="hello_world.xlsx"');
    $writer->save("php://output");

我收到以下错误消息:
    PHP Warning:  ZipArchive::close(): Failure to create temporary file: No such file or directory in /Users/sg/GitWorkingCopies/xxx1/xxx2/library/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php on line 374
    PHP Fatal error:  Uncaught exception 'PhpOffice\PhpSpreadsheet\Writer\Exception' with message 'Could not close zip file php://output.' in /Users/sg/GitWorkingCopies/xxx1/xxx2/library/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php:375

dokumentation of PHPSpreadsheet 说:



upload_tmp_dir 是:/Applications/XAMPP/xamppfiles/temp/
我必须检查哪些文件夹权限?
或者是什么导致了问题?

最佳答案

PHP 中目录写入的一般规则:

必须存在 ,

可由 PHP 进程 写入,

open_basedir php.ini 指令允许它。

因此,在 $writer->save() 方法中设置一些文件路径作为参数并检查是否满足这 3 个规则。

如果您只想在 php://output 方法中使用 php://stdout$writer->save() 值,请检查以下规则:

1) sys_get_temp_dir() 函数返回的目录。在 Windows 中 sys_get_temp_dir() 默认返回当前操作系统用户的临时目录。该值可以通过 sys_temp_dir php.ini 指令更改。

或者

2) upload_tmp_dir php.ini 指令返回的目录。 $useUploadTempDirectory 默认有 false 值。要将其值设置为 true,请在保存文件之前在代码中添加以下行:
\PhpOffice\PhpSpreadsheet\Shared\File::setUseUploadTempDirectory(true);
下面是负责选择保存路径的代码:

\PhpOffice\PhpSpreadsheet\Writer\Xlsx::save 方法( source ):

// If $pFilename is php://output or php://stdout, make it a temporary file...
$originalFilename = $pFilename;
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
    $pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
    if ($pFilename == '') {
        $pFilename = $originalFilename;
    }
}

\PhpOffice\PhpSpreadsheet\SharedFile::sysGetTempDir 方法( source ):
/**
 * Get the systems temporary directory.
 *
 * @return string
 */
public static function sysGetTempDir()
{
    if (self::$useUploadTempDirectory) {
        //  use upload-directory when defined to allow
        // running on environments having very restricted open_basedir configs
        if (ini_get('upload_tmp_dir') !== false) {
            if ($temp = ini_get('upload_tmp_dir')) {
                if (file_exists($temp)) {
                    return realpath($temp);
                }
            }
        }
    }
    return realpath(sys_get_temp_dir());
}

关于PhpSpreadsheet:权限 | ZipArchive::close(): 创建临时文件失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50470370/

10-10 14:50