Support » Plugins » Hacks » Headers outputting file to screen on live but starting download local

  • Hi,
    I’m learning php and to start I have created a plugin for my photography site, The plugin has an admin area and makes use of a shortcode for adding to posts and pages,

    the idea is I sell download codes for the clients images. This plugins admin pages allow you to add a code to the db with a maximum download count and two filenames, the filename on the server (to prevent people from knowing and downloading the file without going through the process) and a filename you want the user to have…

    when you add the shortcode it displays an input box and a button to your pages/posts allowing the viewer to enter the code and if its in the db and the max download count hasn’t been reached iniate the download.

    now all my code works in a basic install of wp locally yet when i move it to the live site to test it does this :

    http://www.ctwo12.com/output.png

    it out puts the content of the file to the screen rather than offering up a download!

    with php outputting enabled it says headers already sent by nav_template.php on line 240 then lists my file and the line number (i think that was it as I’m at work and on my mobile phone!)

    any idea what’s going wrong? And why it works fine locally?

    here are the headers I’m giving :

    $fileonS = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/plugins/photo_dwn_man/downloads/" . $codeRResult;

    //download file (NEEDS MORE LOOKING INTO THIS IS JUST THE BASICS)
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $codeOResult . '.zip');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($fileonS));
    ob_clean();
    flush();
    readfile($fileonS);
    exit;

    I’m here to learn and not copy so please explain any answers so i can take that in and learn from it in future 😊

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It appears the download file request is being processed as a normal page request, causing WP templates to be loaded when they serve no purpose. You probably need a rewrite rule or a different request format to ensure that WP does not try to load templates for this type of request.

    Why did it work on your local installation? This sort of thing usually is the result of different PHP versions running. Just guessing, but perhaps the local version sees this as a warning and keeps executing, whereas the public version sees this as a error and stops executing.

    Why is file content sent to the browser? It may be a side effect of the error and will resolve when the error is resolved. Or it may be a separate issue, possibly related to a charset mismatch. Things tend to work most reliably when the UTF-8 charset is used throughout. If UTF-8 works with your locale, be sure all PHP files are saved with this charset, without the byte order mark, and no trailing spaces prior to line feed and/or carriage return characters. You might also try adding a line similar to this in wp-config.php:
    @setlocale(LC_CTYPE, "en_US.utf8");

    You can use whatever language variant is appropriate, the key is specifying the .utf8 charset.

Viewing 1 replies (of 1 total)
  • The topic ‘Headers outputting file to screen on live but starting download local’ is closed to new replies.