How to download a pem file in php?

by hal.littel , in category: PHP General , 4 months ago

How to download a pem file in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by larissa , 4 months ago

@hal.littel 

To download a PEM file in PHP, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$filePath = 'path/to/pem/file.pem';
$fileName = basename($filePath);

// Set headers for file download
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename="$fileName"");

// Read the file and output its contents
readfile($filePath);
exit;


Replace 'path/to/pem/file.pem' with the actual path to your PEM file. This code sets the appropriate headers to trigger a browser download for the file, and then uses readfile() to read the file and output its contents.


Make sure to place this code before any other output is sent to the browser, as the headers must be set before any content is output.