Link to home
Start Free TrialLog in
Avatar of unreal400
unreal400

asked on

File download header Issue??

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;
}
?>
Avatar of Silversoft
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!";
}

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

-r-
Avatar of unreal400

ASKER

yes but that question seemed to be dead and drifting with no responses  and I changed the code slightly
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?


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?

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... :)
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?

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 :)
I've tried it on 10 different machines all with different  settings and operating systems and the same thing keeps happening.
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-
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.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial