Questions & Answers

Image download works in Xampp but not in Hosting server

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">
    <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>
        <button><a href="download.php">BUTTON</a></button>

The image downloading:


    //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

    //Read the size of the file

    //Terminate from the script


Here is how the files in the code editor are structured:

enter image description here

So, when I execute all that in the Xampp and when the image is downloaded it is alright:

enter image description here

But with all the same code in the hosting when I download and open the image it says this:

enter image description here

What is wrong???

Answers(1) :

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