PhpSpreadsheet:权限| ZipArchive :: close():无法创建临时文件 [英] PhpSpreadsheet: Permissions | ZipArchive::close(): Failure to create temporary file

查看:1588
本文介绍了PhpSpreadsheet:权限| ZipArchive :: close():无法创建临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to offer an Excel-File for download with 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

PHPSpreadsheet的浮现说:

\ PhpOffice \ PhpSpreadsheet \ Writer \ Xlsx在写入php://output时使用临时存储.默认情况下,临时文件存储在脚本的工作目录中.如果没有访问权限,它将退回到操作系统的临时文件位置.

\PhpOffice\PhpSpreadsheet\Writer\Xlsx uses temporary storage when writing to php://output. By default, temporary files are stored in the script's working directory. When there is no access, it falls back to the operating system's temporary files location.

upload_tmp_dir为:/Applications/XAMPP/xamppfiles/temp/

The upload_tmp_dir is: /Applications/XAMPP/xamppfiles/temp/

我必须检查哪些文件夹权限? 还是什么原因导致了问题?

Which folder permissons do I have to check? Or what does cause the problem?

推荐答案

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

General rules for a directory in PHP to write to it:

它必须存在

It must exist,

可通过PHP进程写入

It is writable by PHP process,

open_basedir php.ini指令.

It is allowed by open_basedir php.ini directive.

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

Therefore, set some file path as argument in the $writer->save() method and check that these 3 rules are met.

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

If you want to use only php://output or php://stdout value in the $writer->save() method, check these rules for:

1)sys_get_temp_dir()函数返回的目录.在Windows中,默认情况下,sys_get_temp_dir()返回当前OS用户的临时目录.可以通过sys_temp_dir php.ini指令更改该值.

1) The directory returned by the sys_get_temp_dir() function. In Windows sys_get_temp_dir() by default returns the temporary directory of the current OS user. The value can be changed by sys_temp_dir php.ini directive.

2) upload_tmp_dir

2) The directory returned by the upload_tmp_dir php.ini directive. $useUploadTempDirectory has false value by default. To set it value to true add this line in your code before saving the file:

\PhpOffice\PhpSpreadsheet\Shared\File::setUseUploadTempDirectory(true);

以下是负责选择保存路径的代码:

Here is the code that is responsible for selecting the save path:

通过\PhpOffice\PhpSpreadsheet\Writer\Xlsx::save方法():

// 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方法():

/**
 * 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():无法创建临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆