Why is this not working?

Posted on 2005-04-23
Last Modified: 2011-09-20
Ive got this simple code:

$file = '/home/server/files/76/'.$id;

header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($filename));
header('Content-Disposition: attachment; filename=' . basename($file));

It seems to find the file fine, but it prints out the entire contents.

If I put

$file = '/home/server/files/76/info.gif";

it works fine.

Question by:fox_statton
    LVL 32

    Expert Comment

    Both of them should have worked, since it's actually the exact same thing.

    This is the code I have used for this:
    $file = "/home/server/files/76/".$id;

    $mimetype = "image/gif"; // You should have a switch() block which sets the mimetype for different extensions.
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");    
    header("Content-Type: ".$mimetype);
    header("Content-Length: ".filesize($filename));
    header("Content-Disposition: attachment; filename=\"$id\"");

    LVL 5

    Expert Comment

    try this....

    $file = '/home/server/files/76/'.$id;

    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Description: File Transfer');
    header('Content-Type: image/gif');
    header('Content-Length: ' . filesize($file));
    header('Content-Disposition: attachment; filename="'. basename($file)."\"");
    LVL 3

    Accepted Solution

    $file = '/home/server/files/76/'.$id;
    switch( $file_extension ) {
          case "pdf": $ctype="application/pdf"; break;
          case "exe": $ctype="application/octet-stream"; break;
          case "zip": $ctype="application/zip"; break;
          case "doc": $ctype="application/msword"; break;
          case "xls": $ctype="application/"; break;
          case "ppt": $ctype="application/"; break;
          case "gif": $ctype="image/gif"; break;
          case "png": $ctype="image/png"; break;
          case "jpeg":
          case "jpg": $ctype="image/jpg"; break;
          default: $ctype="application/force-download";
    header("Pragma: public"); // required
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false); // required for certain browsers
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=".basename($file)." )";
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($file));

    hope this helps.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now