简体   繁体   中英

Downloading a zip file to local drive with PHP and WordPress

I am using WordPress for a web site, and would like to include a menu item to download a zip file from the web site to the local drive. I tried using the following function:

function download_binary_file($file) {
    if (file_exists($file)) {
        $base_name = basename($file);
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.$base_name.'"');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
}

But when this function is executed, instead of getting a dialog to save the file, the contents of the zip file are displayed in my browser. Any ideas?

Upload your zip file, then create the menu item, then add a htaccess redirect from the menu item to the zip file url. Zip files usually auto download.. I know there are also download manager plugins for wordpress, that make the process pretty easy.

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