Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do i use php to start a file download ?

    what functions to use ?

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    Here's how i do it for PDF's, modify to suit your needs.

    PHP Code:
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="filename.pdf"');
    readfile('filename.pdf'); 

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Read the comments at http://us2.php.net/header

    It should have all the information you will need.

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have some code at hand.

    Note, this is only SOME of the code you could use;

    $path is something like "files/readme.pdf"

    $file_extension = strtolower(substr(strrchr($filename,"."),1));

    $file = basename($path);
    $size = filesize($path);

    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/vnd.ms-excel"; break;
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpe":
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default: $ctype="application/octet-stream";
    }

    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-Length: $size");
    header("Content-Transfer-Encoding: binary");

    readfile($path);

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Here is what I use
    PHP Code:
    <?php

    $filename 
    $_GET['file'];

    // required for IE, otherwise Content-disposition is ignored
    if(ini_get('zlib.output_compression'))
      
    ini_set('zlib.output_compression''Off');

    // addition by Jorg Weske
    $file_extension strtolower(substr(strrchr($filename,"."),1));

    if( 
    $filename == "" 
    {
      echo 
    "<html><title>Title Here</title><body>ERROR: download file NOT SPECIFIED. </body></html>";
      exit;
    } elseif ( ! 
    file_exists$filename ) ) 
    {
      echo 
    "<html><title>Title Here</title><body>ERROR: File not found.</body></html>";
      exit;
    };
    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/vnd.ms-excel"; break;
      case 
    "ppt"$ctype="application/vnd.ms-powerpoint"; 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");
    // change, added quotes to allow spaces in filenames, by Rajkumar Singh
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    readfile("$filename");
    exit();

    ?>
    And then when you want to a link to download a file you would use
    PHP Code:
    <a href="http://www.domain.com/download.php?file=filename.pdf">download pdf</a
    And make sure this file is saved as download.php and in the same directory as the file you want to download.

    This is just what I use if anyone has something better or a way to improve it let me know.
    Last edited by michaelespinosa; 09-07-2007 at 10:52 PM. Reason: updated


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •