When user tries to download .iso, tar.gz, or .deb from my website php is opening the file instead of downloading it. I'm having a problem with a download script. It runs fine on my local apache server, but when I put it on the my webhost's apache server, it goes crazy. Here's the script:

<?php

$php_scripts = '../../php/';
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';
function mydloader($l_filename=NULL){


    $ip = GetUserIpAddr();
       if (!$pdo = PDOConnect("foxclone_data"))
      { echo "unable to connect";
          exit;
      }

    if( isset( $l_filename ) ) {  

        header('Content-Type: octet-stream');
        header("Content-Disposition: attachment; filename={$l_filename}");
        header('Pragma: no-cache');
        header('Expires: 0');        
        readfile($l_filename);

      }

    else {
        echo "isset failed";
        }  
}
mydloader($_GET["f"]);
exit;

When I say "it goes crazy", I get errors saying that the header values have already been set by apparently by the PDOConnect. Then it actually reads the file. The files being downloaded are .iso, .deb, and .tar.gz. Here's a link to a screenshot:

[img]https://i.imgur.com/2SqiLu4.png[/img]

Fixed by removing blank spaces and moving the db connection code after the headers. Works on both my server and the web host.

Glad you got it fixed. I was going to say, the problem is that you are echo'ing out the "unable to connect" string before calling header(). You can't echo anything to the screen before any header calls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.