How to download a file over https using php?

Member

by dana , in category: Third Party Scripts , 5 months ago

How to download a file over https using php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 2 months ago

@dana 

To download a file over HTTPS using PHP, you can use the cURL library which allows making HTTP requests. Here's an example code snippet to download a file using cURL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$url = 'https://example.com/samplefile.zip';
$filePath = 'path/to/save/samplefile.zip';

$ch = curl_init($url);
$fp = fopen($filePath, 'w');

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);

$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($statusCode == 200) {
    echo 'File downloaded successfully.';
} else {
    echo 'File download failed.';
}

curl_close($ch);
fclose($fp);


Replace the $url variable with the URL of the file you want to download and the $filePath variable with the path where you want to save the downloaded file. This code will download the file from the provided URL and save it to the specified file path.


Note: Make sure the cURL extension is enabled in your PHP configuration.