SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: File Downloads

  1. #1
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File Downloads

    Hi all

    I'm trying to initiate a file download using PHP. I'm using fread() and fopen() to get the file contents and that is working fine. However my code just prints out the contents of the file to the browser rather than actually sending it as a download.

    Here is the code I'm using:

    PHP Code:
            $f fopen($filepath"r");
            
    $file fread($f10000000);


            
    Header("Content-Type: application/ms-download"); 
            
    header("Content-disposition: filename=linktype.zip");
            
    header("Content-Length: " strlen($file));
            echo 
    $file
    I've tried various different MIME types including:
    application/x-tar (zip file)
    application/x-octet-stream (download)

    Thanks in advance

  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No idea if this will help or not, but if you're on windows you'll need to open the file in binary mode before you start reading it (or windows will mess it up). To do that, just use:
    PHP Code:
    $f fopen($filepath"rb"); 
    Hope that helps,

    Skunk

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2000
    Posts
    1,483
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is on a Linux server.

    The file is being read, the browser just isn't recognizing it as a download rather than a document to display.
    Thanks anyway though

  4. #4
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Bookmarks

Posting Permissions

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