File Download Script Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tshrinivasan
    New Member
    • Jan 2008
    • 1

    #1

    File Download Script Problem

    Friends.

    I am doing a file manager application.
    I did the upload part.

    now doing the download script.

    <?php

    $path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // play with the path if the document root does noet exist

    $fullPath = $path.$_GET['download_file'];

    if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullP ath);
    $path_parts = pathinfo($fullP ath);
    $ext = strtolower($pat h_parts["extension"]);
    switch ($ext) {
    case "pdf":
    header("Content-type: application/pdf"); // add here more headers for diff. extensions
    header("Content-Disposition: attachment; filename=\"".$p ath_parts["basename"]."\""); // use 'attachement' to force a download
    break;
    default;
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$p ath_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 your document where the download is shown:
    // <a href="download. php?download_fi le=some_file.pd f">Download here</a>
    ?>


    I got from http://www.finalwebsit es.com/snippets.php?id =4

    The file downloads perfectly.
    but, can not open any file.

    For example, if the downloaded file is "sample.jpg ", I can not open it with any image viewer or firefox.

    but the file size is same as the original.

    how to solve this problem?

    thanks,
    T.Shrinivasan.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    For example, if the downloaded file is "sample.jpg ", I can not open it with any image viewer or firefox
    If you check your headers, you have told the browser the file is a PDF.
    Can you download and open a PDF?.

    Comment

    Working...