Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File download header Issue??

Posted on 2004-11-01
13
Medium Priority
?
242 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
Comment
Question by:unreal400
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 2

Expert Comment

by:Silversoft
ID: 12465754
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
ID: 12466292
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
ID: 12466543
yes but that question seemed to be dead and drifting with no responses  and I changed the code slightly
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:unreal400
ID: 12466879
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
ID: 12467150

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
ID: 12470374
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
ID: 12475038
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
ID: 12480590
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
ID: 12549794
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
ID: 12574473
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
ID: 12668287
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:
modulo earned 0 total points
ID: 12860879
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

610 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