Upload errors
Results 1 to 5 of 5

Thread: Upload errors

  1. #1
    Senior Member
    Join Date
    Sep 2012
    Posts
    269

    Upload errors

    I shamelessly stole this routine from PHP.net:
    PHP Code:
    ob_start();                            
    if (
    file_exists($WriteFile)) {
        
    header('Content-Description: File Transfer');
        
    header('Content-Type: application/octet-stream');
        
    header('Content-Disposition: attachment; filename='.basename($WriteFile));
        
    header('Content-Transfer-Encoding: binary');
        
    header('Expires: 0');
        
    header('Cache-Control: must-revalidate');
        
    header('Pragma: public');
        
    header('Content-Length: ' filesize($WriteFile));
        
    ob_clean();
        
    flush();
        
    ob_flush();
        
    readfile($WriteFile);
        exit;

    $FileName is set at the beginning of the script.

    The routine never calls up a 'save' dialogue. Instead, it writes the contents of the file at the bottom of the script's output. I've got error reporting turned on and there are no errors where the upload code is.
    Last edited by NogDog; 06-23-2013 at 02:39 AM.

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,771
    You can't serve two responses for one request. You need a script that serves only the file (with the appropriate http headers).
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,816
    Do you have all error reporting turned on to see if perhaps the header() commands are failing (in spite of your best efforts with ob_*(), etc.)? Or just use your browser tools to inspect what headers were received.

    Or you could explicitly check:
    PHP Code:
    if(headers_sent()) {
        throw new 
    Exception("Headers already sent!");
    }
    // start your header() calls... 
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  4. #4
    Senior Member
    Join Date
    Sep 2012
    Posts
    269
    So, I moved the header routine to a script all by itself. I was still getting the error. Somewhere I blundered on a site that said to place all 'echo' statements below the header section. So I did and it worked.

  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,349
    Quote Originally Posted by timstring View Post
    Somewhere I blundered on a site that said to place all 'echo' statements below the header section. So I did and it worked.
    Was it php.net, by any chance? Specifically, the manual page for header():

    Quote Originally Posted by PHP Manual
    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
    ?

    EDIT: Also.. I'm guessing these echo statements were somewhere above the lines you posted? I didn't see anything in your code that sends output before the header() - hence why no one suggested that.

    Also, if this was indeed the culprit, you should have received warnings in your PHP error log to the tune of :
    Warning: Cannot modify header information – headers already sent by (output started at /path/to/offending/script.php:123)
    Last edited by bradgrafelman; 06-24-2013 at 06:50 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •