How to Force Image Download in PHP From a Link

Force image download is mandatory in some content downloading websites to provide a good and smooth user experience. Even more, sometimes we have to allow direct download an image from a link.

If we provide an image link to the user for downloading, the browser will open that image in its self rather than downloading it. No one likes to open another website link from their website.

To resolve this issue we can provide another PHP file link that can force download an image from a link to the user’s browser.

Read Also: Force Download File Using PHP

Force Download an Image Using PHP

//file path in server
$file_path = "";

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file_path).'"');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($file_path));

// Clear output buffer


We can download any image file hosted on any server using the above PHP code. Even more, we can force download images from our own server.

Happy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
Compress Image Size Without Losing Quality in PHP
How to Compress Image Size Without Losing Quality in PHP

PHP provides the ability to compress images without losing the quality of it. Even more, we can resize images in...