简体   繁体   中英

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">
<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM