1

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);

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, adding the Content-Disposition header worked perfectly.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.