When user tries to download .iso or .deb from my website php is opening the file instead of downloading it

Links for php scripts

Moderators: gesf, Michalio

Post Reply
Larryfox

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:

Code: Select all

<?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 screenshot:

Image

Any help is appreciated.
Larryfox

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

Thanks for posting the fix! Bound to help someone running into a similar issue down the line.
Post Reply