Jump to content

[SOLVED] help with download script using headers


nuttycoder

Recommended Posts

Hi am tying to downlload images from my site using headers here the code am using:

 

$target = 'assets/images/site/logo.png';
$type = 'image/png';
$name = 'logo.png';
$size = filesize('assets/images/site/logo.png');


//offer file for download using headser
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=$name");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
readfile($target);

 

When I run the file I download the image but the image is corrupt anyone know where I'm going wrong?

Try this (take from http://us.php.net/readfile)

 

<?php
$file = 'assets/images/site/logo.png';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.