Login | Register | FAQ
Anonymous

PHP download script error.

+ Post a reply

2 posts Page 1 of 1


PHP download script error.

by harvey » Mon Apr 23, 2012 9:47 pm

I have been trying to create a download script this is a simple version of what I have but in both versions I get the same error
In my php file I have :
<?php
header('Content-disposition: attachment; filename=card.png');

header('Content-type: image/png');

readfile('card.png');
?>
And then the clickable download link: <a href="FileDownload.php">Download</a>

Every thing seems to work and the file downloads but I can not view it
I get the following,when opening the picture Windows Photo Viewer can't open this picture because either Photo viewer doesn't support this file format or you don't have the latest version.
Note im using windows.

Can anyone help??

Thanks in advance
Harvey
User avatar

harvey

  • Posts: 1
  • Joined: Mon Apr 23, 2012 9:32 pm

Re: PHP download script error.

by XainPro » Wed Apr 25, 2012 4:48 pm

harvey, your code is some how not complete you can use this script

Code: Select all
<?php

// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>
User avatar

XainPro

  • Posts: 3536
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1