Solved

File download header Issue??

Posted on 2004-11-01
239 Views
Last Modified: 2008-03-04
I have a php function that is meant to download a file when I post the filename to a new page.  Basically I have a file browser window that I've created to securely access certain files outside of the webroot.   I can get the files names pass the files over and they will download.    The problem I am having is this:

There will be 2 save or open screens when I download the file before it will actually download.   The other problem is I can't get the processing page to close itself once the download is complete.   I have included the code I am using to do the download.   Can anybody tell me what is wrong with this code that is causing it to react the way it is? Are the headers somehow wrong or is it a windows issue?

Thanks

Kelly



<?
DownloadFile($filetoget);
exit();
?>


function DownloadFile($filename,$Download = 1)
{
     // Check filename
     if (empty($filename) || !file_exists($filename))
     {
         return FALSE;
     }

     // Create download file name to be displayed to user
     $saveasname = basename($filename);
     
     // Fix for SSL in IE
     header("Pragma: ");      
     header("Cache-Control: ");
     
     if($Download)
                {
          header('Content-Type: application/octet-stream'); // Send binary filetype HTTP header
                }
     else
                {
                                // If you want it to just open in the browser instead of prompting to download,
                                // send the proper content type.
          header('Content-Type: application/pdf'); // Send PDF filetype HTTP header
                }
     
     // Send content-length HTTP header
     header('Content-Length: '.filesize($filename));
     
     // Send content-disposition with save file name HTTP header
     // (using workaround for MSIE 5.5 SP1 / MSIE 6.0 bugs/problems)
     if($Download)
     {
          if (IsSet($HTTP_USER_AGENT) && (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) || preg_match('/MSIE 6.0/', $HTTP_USER_AGENT)))
          {
              header('Content-Disposition: filename="'.$saveasname.'"');
          }
          else
          {
              header('Content-Disposition: attachment; filename="'.$saveasname.'"');
          }
     }
     else
     {
          header("Content-Disposition: inline; filename=\"" . $saveasname . "\"");
     }
     
     // Send Content-Transfer-Encoding HTTP header
     header('Content-Transfer-Encoding: binary');
     
     // Output file
     readfile($filename);
     
     // Done
     return TRUE;
}
?>
0
Question by:unreal400
    12 Comments
     
    LVL 2

    Expert Comment

    by:Silversoft
    Hi

    Not too sure if this is helpful, but this is what i use for secure downloads in PHP

    <?
    if ($_SESSION['user_login'] == true) //check if you login to download the file
    {
            $dir = "../download/"; //the directory is outside the web root folder, not accessible through web browsers
            $file= $dir.$_GET['file'];
            if (file_exists($file))
            {
                  header("Content-type: application/force-download");
                  header("Content-Transfer-Encoding: Binary");
                  header("Content-length: ".filesize($file));
                  header("Content-disposition: attachment; filename=".basename($file). "");
                  readfile("$file");
            }
            else
            {
                  echo "PDF file does not exisit!";
            }
    }
    else
    {
           echo "Access Denied!";
    }

    ?>
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    Isn't this exactly the same question as you asked last week, and asked to refund earlier today?

    -r-
    0
     
    LVL 2

    Author Comment

    by:unreal400
    yes but that question seemed to be dead and drifting with no responses  and I changed the code slightly
    0
     
    LVL 2

    Author Comment

    by:unreal400
    Silversoft unfortunately that code does not work  it does the same thing asking to open the file twice for some reason.
    the window will pop up and ask  to open or save.  I click open  then the exact same thing happens again and it opens the second time.

    Would it be the fact that I am posting to another page to do this be a factor in it asking twice?

    0
     
    LVL 2

    Author Comment

    by:unreal400

    One other thought.  this works perfectly for when you save ethe file. It brings up the save file location downloads it then asks you if you want to open, open folder or close. Just like it should.

    so if they save the file my code works perfectly.  Its just on an open this occurs. Silversoft  how does your react to using open instead of save?

    Is there a good place where I get get information on what the different headers mean?

    0
     
    LVL 2

    Expert Comment

    by:Silversoft
    Hi Unreal

    With the headers i use, save the file is works fine and if open file is selected instead of save, it will say downloading to temporary folder and then open the file accordingly, so no problem at all.

    Here are some header articles, hope you find something useful there:

    http:Q_20879082.html
    http://www.zend.com/manual/function.header.php
    http://www.php.net/header

    good luck... :)
    0
     
    LVL 2

    Author Comment

    by:unreal400
    I found out one other piece of information.

    On the machines that werent working   if I went into  outlook and opened and attachment and checked off the option of ask before opening   then in my windows the files would open up with only one  confirmation box coming up.  so that would explain why its been working on mine.  But if the user has never unchecked that option in outlook it will always come up with two boxes for the open.

    Even with that being discovered  the window I post to still will not close it self when it is done.  so the file will download then I have a blank window sitting there that the user would have to close each time they download a file.   How can I go about solving that problem?

    0
     
    LVL 2

    Expert Comment

    by:Silversoft
    Hi Unreal

    I think the problem you have here might not be a PHP header issue but rather a client machine setting issue. Not too sure
    Maybe if possible you should try my piece of code I posted above to see if it a client machine setting hat is causing the problem or
    it a PHP header problem. So if my code works fine then it a client machine problem else then it's your PHP header code problem.
    Maybe just try that and see.

    good luck :)
    0
     
    LVL 2

    Author Comment

    by:unreal400
    I've tried it on 10 different machines all with different  settings and operating systems and the same thing keeps happening.
    0
     
    LVL 2

    Expert Comment

    by:Silversoft
    Hi unreal

    sorry to reply to this thread so late, have been very busy.

    Regarding your problem, I am not too sure how to solve your question :( As the headers i use for file downloads works perfectly on my machine and many other machines that i've worked with. So it may be not an PHP issue, but rather a server config or client setup issue.

    My suggestions:

    1.) Post a new question in this TA again and/or other TA like Apache server and have the question updated for other experts to help you solve your problem.

    2.) Regarding this question, if you find the comments of this thread has not helped you at all, you can get your points refunded by going though to CS and get this question deleted. If however you do find some comments in this thread has helped you in someway, you can reward some effort points or partial answer point and get this question closed.

    regards-
    0
     
    LVL 2

    Author Comment

    by:unreal400
    I've disconverd that this is a flaw with microsoft office XP  it is a programmed thing.   Its in microsofts information that is a programmed action and cannont be removed.
    0
     

    Accepted Solution

    by:
    PAQed with points refunded (500)

    modulo
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: Foundations of Front-End Development

    Jump-start a lucrative career in front-end web development, with zero previous coding experience required. This course covers the basic programming concepts and languages required for creating engaging websites from scratch.

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now