Backend / frontend get files to zip and download


(Perrouddavid) #1

Hello,
I need a little help here ^’^

I have a form in frontend where client add a “publication” with images. When the form is submitted, a folder (inside frontend/web/uploads/) is created with the name of the publication, then the images are saved in it. Until here no problem.

Now, i try, from the backend, to create a button “Download the Zip” that should get all the images from this publication, create a zip folder and download it.

there is my function (in common/models/Files) so in my backend controllers i can just use the same function and pass it the $files.

$files is = to $model->files (relational) as ia have a Files table linked with my differetns “publications”

public function zipMyFiles($files){

    $file_folder = '/frontend/web/';
    if(extension_loaded('zip')){
        if(!empty($files)){
            $zip = new \ZipArchive();
            
            $zip_name = time().'.zip';

            if($zip->open($zip_name, \ZipArchive::CREATE) !== TRUE) {
                throw new \Exception('Cannot create a zip file');
            }

            foreach($files as $file){
                $zip->addFile($file_folder.$file->file_url);
                
            }

            $zip->close();

            if(file_exists($zip_name)){
                header('Content-type: application/zip');
                header('Contant-Disposition: attachment; filename="'.$zip_name.'"');

                readfile($zip_name);
                unlink($zip_name);
                 echo 'yes';
            }else{
                echo 'error';
            }
        }
    }
    
}

i keep getting the echo ‘error’, but i don’t find what is wrong…

if i do a var_dump of $files i get this :

array(2) { [0]=> object(common\models\Files)#152 (10) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(6) { ["id"]=> int(22) ["type_id"]=> int(12) ["file_name"]=> string(8) "coke.png" ["file_url"]=> string(59) "uploads/vmproducts/produit_du_soir/produit_du_soir-coke.png" ["date_creation"]=> string(19) "2019-09-26 08:05:04" ["date_modification"]=> string(19) "2019-09-26 10:05:04" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(6) { ["id"]=> int(22) ["type_id"]=> int(12) ["file_name"]=> string(8) "coke.png" ["file_url"]=> string(59) "uploads/vmproducts/produit_du_soir/produit_du_soir-coke.png" ["date_creation"]=> string(19) "2019-09-26 08:05:04" ["date_modification"]=> string(19) "2019-09-26 10:05:04" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_eventWildcards":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } } [1]=> object(common\models\Files)#162 (10) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(6) { ["id"]=> int(25) ["type_id"]=> int(12) ["file_name"]=> string(12) "img_1278.jpg" ["file_url"]=> string(63) "uploads/vmproducts/produit_du_soir/produit_du_soir-img_1278.jpg" ["date_creation"]=> string(19) "2019-09-26 08:09:33" ["date_modification"]=> string(19) "2019-09-26 10:09:33" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(6) { ["id"]=> int(25) ["type_id"]=> int(12) ["file_name"]=> string(12) "img_1278.jpg" ["file_url"]=> string(63) "uploads/vmproducts/produit_du_soir/produit_du_soir-img_1278.jpg" ["date_creation"]=> string(19) "2019-09-26 08:09:33" ["date_modification"]=> string(19) "2019-09-26 10:09:33" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_eventWildcards":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } } }

(Auzadventure) #2

I suspect this has to do with the pathing from the /backend

You might need to use

Url::to('@frontend')

As your files are stored in the frontend/web/uploads but you are accessing it from the /common or /backend. Check your paths