Introduction
In my personal project i am using:
- Symfony v4.2.3;
- PHP v7.2.14
- OnUp Flysystem bundle;
- Flysystem ZipArchive.
- on Windows 10 Pro (php built-in dev server);
File operations with Flysystem works fine both in project_dir/public
folder and in project_dir/data
folders.
Problem
When i try to create ZIP archive in public
directory there is an error: Could not open zip archive at:zip:\\test123\my_zip_test.zip, error: 5
.
My code
Controller that creates ZIP file
<?php
namespace App\Controller;
use App\UltraHelpers\UltraAccess;
use App\UltraHelpers\UltraWhereabouts;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\MountManager;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
class AdminZipController extends BaseController
{
/**
* @Route("/zip", name="zip")
*/
public function createZipArchive(Request $request, SessionInterface $session, MountManager $mountManager, ContainerInterface $container)
{
$kernel_project_dir = $container->getParameter('kernel.project_dir');
$adapter_public = new Local($kernel_project_dir .'/public/uploads/');
$adapter_zip = new ZipArchiveAdapter($kernel_project_dir .'/public/uploads/');
$filesystem_public = new Filesystem($adapter_public);
$filesystem_zip = new Filesystem($adapter_zip);
$mountManager->mountFilesystem('public', $filesystem_public);
$mountManager->mountFilesystem('zip', $filesystem_zip);
$public_path = 'public://test123/';
$public_zip = 'zip://test123/my_zip_test.zip';
$adapter_zip->openArchive($public_zip);
// get all files in directory
$files_2_zip = $mountManager->listContents($public_path, false);
// itereate trough all files in directory
foreach ($files_2_zip as $object)
{
$mountManager->copy($object['path'], $public_zip);
}
$adapter_zip->getArchive()->close();
return $this->render('default/create_zip_archive.html.twig', []);
}
}
Update 1
As ZIP file creation goes on in public
folder, there should be no concerns about lack of access.
Update 2
As i am using Flysystem mount manager
to easily manage files across same filesystem but different locations i would like to use the same setup also for ZIP files.
Update 3
Found that ZipAdapter
uses PHP ZipArchive
.
There are error codes in documentation.
So my problem: error 5 = read error
Finally
Am i missing something?
Thank you for your ideas and suggestions!