I'm trying to download an image JPG using PHP, I have successfully made it work in Xampp via localhost but when I try to do the same in my hosting it doesn't work (I have a Jetthost hosting with basically the same configuration of the Xampp server), here is the code:
The download button:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Button download</title> </head> <body> <button><a href="download.php">BUTTON</a></button> </body> </html>
The image downloading:
<?php //Read the filename $filename = 'image/balloons.jpg'; //Define header information header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header("Cache-Control: no-cache, must-revalidate"); header("Expires: 0"); header('Content-Disposition: attachment; filename="'.basename($filename).'"'); header('Content-Length: ' . filesize($filename)); header('Pragma: public'); //Clear system output buffer flush(); //Read the size of the file readfile($filename); //Terminate from the script die(); ?>
Here is how the files in the code editor are structured:
So, when I execute all that in the Xampp and when the image is downloaded it is alright:
But with all the same code in the hosting when I download and open the image it says this:
What is wrong???
I tried your code on my local laptop and server side as well, it works fine. I think while uploading to the server you may have forgotten to upload the image or there might be some spelling mistake in your image name.
You can check the below link for your reference https://socialbeast.in/imgdownload/