PHP ZipArchive Not Working in Laravel

BangularTK picture BangularTK · Nov 9, 2016 · Viewed 11.8k times · Source

PHP ZipArchive is working perfectly if I run it in raw php, but getting a "class not found" error when I try to run it in my Laravel project:

FatalErrorException in WidgetController.php line 40: Class 'App\Http\Controllers\ZipArchive' not found

Here's the function I have in my laravel controller:

public function installHello()
{
    $file_path = base_path("resources/assets/packages/helloworld.zip");

    $zip = new ZipArchive;
    if ($zip->open($file_path) === TRUE) {
        $zip->extractTo(base_path('packages/tkabir/'));
        $zip->close();
        return redirect()->back();
        //echo 'ok';
    } else {
        echo 'failed';
    }
}

And here's the sample I tried in an index.php file:

<?php
$zip = new ZipArchive;
if ($zip->open('E:/xampp/htdocs/ziptest/helloworld.zip') === TRUE) {
    $zip->extractTo('E:/xampp/htdocs/ziptest/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

Any idea why it wouldn't work in laravel?

Answer

BangularTK picture BangularTK · Nov 9, 2016

Problem solved. Made an obvious mistake: forgot to ''use ZipArchive'' in my Laravel controller