0

I am trying to make a plugin which will force a certain file to download. I cannot find any examples of other people downloading files using a wordpress plug in. I simplified the code a little bit. I believe everything works until this part:

  header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="'.$destination.'"');
    echo readfile($destination);
    // remove zip file is exists in temp path
    unlink($destination);

Here is the code:

    function get_cart_urls(){
//get a string of urls from an ajax request

       $urlsCSV= $_POST['urlsCSV'];

//convert the string into an array
       $urlArray= explode(',', $urlsCSV);

        $destination = "Files.zip";
        $files = $urlArray;
        $overwrite = true;

            //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { $error .= "yeha ";}
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists(getcwd().$file)) {
                $valid_files[] = getcwd().$file;
            }
        }
    }
    //echo count($valid_files);
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();

        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            $error = "yeah ";
        }else{
            $error = "no error";    
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file,$file);
        }
        //debug
        echo $error." ".'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

        //close the zip -- done!
        $zip->close();


    if(file_exists($destination)){
            // push to download the zip
            header('Content-type: application/zip');
            header('Content-Disposition: attachment; filename="'.$destination.'"');
            echo readfile($destination);
            // remove zip file is exists in temp path
            unlink($destination);
        }
    die();
  • Is there a question somewhere? What about an actual problem description? And maybe some hints whether you are really using the code as is, especially with the debug echo. And finally, where exactly is this code being invoked? – ndm Aug 23 '14 at 19:16
  • I've used the plugin Customer Area which allows admins to upload files, and their users to download files. That could be an example (as you say you found none) – Pat Aug 23 '14 at 21:57
  • Can you make it work in general (without WP) for starters? This seems a little generic by itself so it's hard to guess how WP interacts with it, if at all. – Rarst Aug 25 '14 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.