1

I have installed phpoffice/phpword package on laravel to export multiple files using a foreach loop inside my controller (please refer to code below). Each file is named after by the according id number, eg. 15_en.doc, 16_en.doc etc, and all files are automatically stored inside laravel\storage folder.

//Controller excerpt 

foreach($regs as $i) 
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('Ariel');

//code

//saving results:
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
 try 
    {
        $objWriter->save(storage_path(''.$i->id.'_en.docx'));
        continue;
    }  
    catch (Exception $e) 
    {
    }

    return response()->download(storage_path(''.$i->id.'_en.docx'));

  }

Now I want to have all files zipped. For this reason I installed zanysoft/laravel-zip package. I have edited my code as below. This creates a zip file inside laravel's public folder, including storage default project subfolders. How can I exclude them? And secondly, when the project goes live, how can users be prompted to download .zip file?

Thank you!!

//Controller excerpt 

foreach($regs as $i) //
{

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->setDefaultFontName('Ariel');

//code

...

//code

//saving results:

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
try 
    {
        $objWriter->save(storage_path(''.$i->id.'_en.docx'));
           continue;

    }  
catch (Exception $e) 
    {
    }

    return response()->download(storage_path(''.$i->id.'_en.docx'));

}  //@forech ends here

$zip = Zip::create('your_zipfile.zip');   // this creates zip file name
$zip->add('c:\xampp\htdocs\laravel\storage'); //this zips files exported and the rest of subfolders!
Konstantinos
  • 51
  • 1
  • 14

1 Answers1

0

The solution is here:

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    try 
        {

            $objWriter->save(storage_path('zipped_en\'.$i->id.'_en.docx'));  
            continue;

        } 
        catch (Exception $e) 
            {
            }

            return response()->download(storage_path('zipped_en\'.$i->id.'_en.docx'));   
            continue;

} //@forech ends here



$we = Carbon::now(); //I want to add date and time creation attribute to filename created
$we = Carbon::createFromFormat('Y-m-d H:i:s', $we)->format('dmYHis');


$zip = Zip::create('zipped_en_'.$we.'.zip'); 



$zip->add('c:\xampp\htdocs\laravel\storage\zipped_en');
Konstantinos
  • 51
  • 1
  • 14