简体   繁体   中英

Auto download CSV file after link is clicked

I am using PHP to create csvs files, put them into a folder and then zip the folder. This all works perfectly however I can't then get the ZIP the user created to auto download.

EDIT

it now works after taking out a redirect, however it lacks the .zip extension when being downloading, is there a way to force this?

My zips are stored in /zips in the root

Here is my code

 // Add the folder we just created to a zip file.
$zip_name = 'groups_' . time();
$zip_directory = '/zips';
$zip = new zip( $zip_name, $zip_directory );
$zip->add_directory( 'group-csvs' );
$zip->save();

$zip_path = $zip->get_zip_path();

header( "Content-Description: File Transfer" );
header( "Content-type: application/zip" );
header( "Content-Disposition: attachment; filename=" . $zip_name . "");
header( "Content-Length: " . filesize( $zip_path ) );

readfile($zip_path);

Here is the get path method and constructor in my zip class

   public function __construct( $file_name, $zip_directory)
   {
        $this->zip = new ZipArchive();
        $this->path = dirname( __FILE__ ) . $zip_directory . $file_name . '.zip';
        $this->zip->open( $this->path, ZipArchive::CREATE );
    }

   /**
     * Get the absolute path to the zip file
     * @return string
     */
    public function get_zip_path()
    {
        return $this->path;
    }   

I don't get any errors, it just creates the zip then nothing happens

Add .zip extension to the name of your file: $zip_name = 'groups_' . time() . '.zip'; $zip_name = 'groups_' . time() . '.zip'; The extension you are using in your custom zip class isnt being appended to variable in your main file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM