skip to Main Content

I am using Laravel nativePHP package to generate desktop application

It’s running fine but when I try to generate zip file with ZipArchive class, getting this error : Class "ZipArchive" not found

This is my controller code :

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesStorage;
use Response;
use IlluminateSupportFacadesFile;
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()]);
        }
    }
}

Actully, This code working fine on web browser but in nativePHP application showing error. When I have checked Zip extension is enabled or not by echo phpinfo();

In nativePHP application is not showing but on web browser showing enabled.

and also showing different php version for both nativePHP(8.2.8) or web browser(8.2.14)

So how to enable php-zip extation for laravel nativePHP aplication ?

phpinfo output On web browser :

enter image description here

phpinfo output On nativePHP Application :

enter image description here

How to fix this error(Class "ZipArchive" not found) and enable php-zip for nativePHP application ?

Thank you!

2

Answers


  1. You can update the php version to the one with php-zip enabled by changing the php version used by the cli with:

    sudo update-alternatives --config php
    

    More information about updating the php cli version

    Login or Signup to reply.
  2. NativePHP’s built-in binaries now include the zip extension by default 🎉

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search