Jump to content

force a download in IE fails HEELLP!!


jagguy

Recommended Posts

OK this  fails to download a file in IE and I have no idea as to why this does. What happens is that it downloads the same php file instead of the required file like it should and does do in FF. FF works fine.

 

Why does IE fail to download this from this script and i really need this working ASAP. I can't believe that IE does this and yes i am panicking.

I started a new thread as my other threads were getting messy and old on this topic. I just found out about this and HELP

$file_extension = strtolower(substr(strrchr($file,"."),1));
if ((isset($file))&&(file_exists('files/'.$file)))
{


switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}

$ary_header = get_headers($file, 1);
$filesize = $ary_header['Content-Length'];

// fix for IE catching or PHP bug issue

header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");


header("Content-type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

header("Content-Transfer-Encoding: Binary");
// @header("Content-length: ".filesize($file_path));
header("Content-length: ".$filesize);
header("Content-Type: $ctype");

//header("Content-Disposition: attachment; filename=".basename($filename).";");


header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$file_path");

}
else
echo "Error No file selected<br><a href='. $myvar.'/download.php>Back</a>";

Link to comment
https://forums.phpfreaks.com/topic/61215-force-a-download-in-ie-fails-heellp/
Share on other sites

<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";
  exit;
} elseif ( ! file_exists( $filename ) ) 
{
  echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";
  exit;
};
switch( $file_extension )
{
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

?>
    

what happens is that it will work for a txt file in IE/FF but fails for a doc file in IE.

 

FF works fine.

 

 

Also if I use a download manager then this picks up the same  php file which it shouldn't .

 

 

     $file_extension = strtolower(substr(strrchr($file,"."),1));
     if ((isset($file))&&(file_exists('files/'.$file)))
       {

         if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');
  
         switch( $file_extension )
          {
          case "pdf": $ctype="application/pdf"; break;
          case "exe": $ctype="application/octet-stream"; break;
          case "zip": $ctype="application/zip"; break;
          case "doc": $ctype="application/msword"; break;
          case "xls": $ctype="application/vnd.ms-excel"; break;
          case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
          case "gif": $ctype="image/gif"; break;
          case "png": $ctype="image/png"; break;
          case "jpg": $ctype="image/jpg"; break;
          default: $ctype="application/force-download";
          }

           $filename = $HTTP_GET_VARS['path'];

             // fix for IE catching or PHP bug issue
             header("Pragma: public"); // required
             header("Expires: 0");
             header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
             header("Cache-Control: private",false); // required for certain browsers

           // force download dialog
             header("Content-Type: application/force-download");
           //header("Content-Type: application/octet-stream");
             header("Content-Type: application/download");
             header("Content-Type: $ctype");

             //header("Content-Disposition: attachment; filename=".basename($filename).";");

             header('Content-Disposition: attachment; filename="' . $file . '"');

             header("Content-Transfer-Encoding: binary");
             header("Content-Length: ".filesize($file_path));

             readfile($file_path);

I don't think you have read my code as i have done this to the nth degree already.

 

look in my code

case "doc": $ctype="application/msword"; break;

..

 

and the filename will have a .doc at the end for readfile.

 

****The problem is that it fails to work in IE using a doc file BUT works in FF.

If I download my.doc in FF it works . If I download my.doc in IE it fails to have any data in the filename.

 

My download manager gets a php file as well when there is none to download. This is really screwy?

throw all the .doc in a zip then let them download it then or

 

use this works for me sorry can not help otherwise.

 

ps. there are meny bugs in ie from one version to another.

 

Header( "Content-Type: application/octet-stream" );

Header( "Content-Length:" . filesize( $filepath ) );

Header( "Content-Disposition: attatchment; filename=$filename" );

readfile( $filepath );

 

If I take out this security check the download works. IE and my download manger kept picking up login.php.

I don't actually need this check as any wrong file path or sql injection can be dealt with.

Why do I need to do this though?

 

 

if (isset($_SESSION['HTTP_USER_AGENT']))

{

    if ($_SESSION['HTTP_USER_AGENT'] != md5($_SERVER['HTTP_USER_AGENT']))

    {

 

        session_destroy();

        header( "Location:". $myvar."/login.php" );

        exit;

 

    }

}

else

{

    $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);

}

 

if (!isset($_SESSION['uid']))

  {

    header( "Location: ". $myvar."/login.php" );

    exit;

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.