如何在 laravel nativephp 应用程序中启用 php-zip

问题描述 投票:0回答:1

我正在使用 Laravel nativePHP 包来生成桌面应用程序

它运行良好,但是当我尝试使用 ZipArchive 类生成 zip 文件时,出现此错误:

Class "ZipArchive" not found

这是我的控制器代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Response;
use Illuminate\Support\Facades\File;
use ZipArchive;

class Home extends Controller
{
    /**
     * Show form
     *
     */
    public function index()
    {
        return view('generate-zip');
    }

    public function generateZip(Request $request)
    {
        try {
            $file = $request->file('fileInput');
            $fileContents = file($file->getPathname());
            $folderPath = $request->textInput;

            $destination = public_path($folderPath.'/selected-images.zip');
            $zip = new ZipArchive();
            $zip->open($destination, ZipArchive::CREATE);

            foreach ($fileContents as $line) {
                $data = str_getcsv($line);
                $imgName = $data[0];
                if ($imgName) {
                    $localFilePath = $folderPath.'/'.$imgName;
                    $file = public_path($localFilePath);

                    if (File::exists($file)) {
                        $zip->addFile($file, $imgName);
                    }
                }

            }
            $zip->close();

        } catch (\Exception $e) {
            return redirect()->back()->withError([$e->getMessage()]);
        }
    }
}

实际上,这段代码在网络浏览器上运行良好,但在本机PHP应用程序中显示错误。当我通过 echo phpinfo() 检查 Zip 扩展名是否启用时;

在本机PHP应用程序中未显示,但在网络浏览器上显示已启用。

还显示本机PHP(8.2.8)或网络浏览器(8.2.14)的不同php版本

那么如何为 Laravel NativePHP 应用程序启用 php-zip 扩展?

phpinfo 在网络浏览器上输出:

nativePHP 应用程序上的 phpinfo 输出:

如何修复此错误(未找到类“ZipArchive”)并为本机PHP应用程序启用php-zip?

谢谢!

php laravel ziparchive php-zip-archive native-php
1个回答
0
投票

您可以通过更改 cli 使用的版本来将 php 版本更新为启用了 php-zip 的版本:

update-alternatives --config php

有关更新 php cli 版本的更多信息

© www.soinside.com 2019 - 2024. All rights reserved.