Search code examples
phpwoocommercedownloadphp-ziparchive

readfile does not do the auto download in php


NOTE: this is an modidied question

I've created a zip file where i've put some images. Now I want to download this when clicked in a button.
It works fine for creating and putting the images I want in the folder. But the download dosen't work.

$zip = new ZipArchive();
                $zip->open("zipped_ordered_images_$order_id.zip", ZipArchive::CREATE);
                foreach($images_url as $image_url) {
                    $single_image = $image_url;
                    $download_file = file_get_contents($image_url);
                    $zip->addFromString(basename($image_url),$download_file);
                }
                $zip->close();

                if (file_exists("zipped_ordered_images_$order_id.zip")) {
                    header('Content-Type: application/zip');
                    header('Content-Disposition: attachment; filename="zipped_ordered_images_'.$order_id.'.zip"');  
                    readfile('zipped_ordered_images.zip'); // auto download
                    unlink('zipped_ordered_images.zip');
                }

Solution

  • You would need to eject from Create React App and then modify your webpack config to allow it to search directories other than src/