使用PHP CURL下载文件

I am trying to download a .exe file using PHP CURL from the web server to the PC, using CURL to make the webserver login automatic. The problem I'm having is that the file is being written to the browser window instead of asking the user to save or run the file. I'm using the code below, how can I fix this to make it work like I want?

$username = "myuser";
$password = "mypassword";
$url="http://www.mywebsite.com/files/softwaredownload/myfile.exe";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_exec ($ch);
curl_close($ch);

Add to your code these lines:

curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

... and replace curl_exec($ch) with $response = curl_exec($ch);, so $response content might be saved into a local file with file_put_contents() of fwrite() methods.

You should set the headers so the browser knows it has to display the save dialog.

Content-Disposition: attachment; filename=<file name.ext>

Also, bear in mind that Content-Type header should be before Content-Disposition.