alfiyan28
alfiyan28

Reputation: 11

Create Download Link PHP

I had a problem with my code . when I click the button to download the picture , it appears the code that I do not know, how to solve this ? what is wrong with my code ?

--> Download.php

if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
    $filename = $_GET['file'];
} else {
    $filename = NULL;
}


// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';

if (!$filename) {
    // if variable $filename is NULL or false display the message
    echo $err;
} else {
    // define the path to your download folder plus assign the file name
    $path = '../images/upload/lowongan_pekerjaan/'.$filename;
    // check that file exists and is readable
    if (file_exists($path) && is_readable($path)) {
        // get the file size and send the http headers
        $size = filesize($path);
        header('Content-Type: application/octet-stream');
        header('Content-Length: '.$size);
        header('Content-Disposition: attachment; filename='.$filename);
        header('Content-Transfer-Encoding: binary');
        // open the file in binary read-only mode
        // display the error message if file can't be opened
        $file = @fopen($path, 'rb');
        if ($file) {
            // stream the file and exit the script when complete
            fpassthru($file);
            exit;
        } else {
            echo $err;
        }
    } else {
        echo $err;
    }
}

why show like this? My Picture

whats wrong with my code?

Upvotes: 1

Views: 1641

Answers (1)

Thomas B
Thomas B

Reputation: 71

    header('Content-Type: application/octet-stream');
    header('Content-Length: '.$size);
    header('Content-Disposition: attachment; filename='.$filename);
    header('Content-Transfer-Encoding: binary');

should be

    header('Content-Type: image/jpg');
    header('Content-Length: '.$size);
    header('Content-Disposition: attachment; filename='.$filename);

or png or gif or whatever your file type is.. the browser does not know how to handle what you are sending it because application/octet-stream != an image type.

Upvotes: 1

Related Questions