Results 1 to 6 of 6

Thread: [resolved] ignore mime types

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    [resolved] ignore mime types

    how can I have my php script just send a file to be downloaded, ignoring the mime type (i suppose)

    i'm trying to setup my server to allow people to download an mp3 (it's totally legal... btw) and the browser tries to play the mp3 instead of allowing it to be saved...


    here is the code

    PHP Code:
    <?php
      
    function DownloadFile($file)
        {
          
    define('FILEDIR''/music/'); 
          
    $path FILEDIR $file
      
          
    //check that this file exists and that it doesn't include 
          //any special characters 
          
    if(!is_file($path) OR !eregi('^[A-Z_0-9][A-Z_0-9.]*$'$file)) 
          { 
              print 
    "Requested Path: $path<br>";
              print 
    "Is a File: " is_file($path) . "<br>";
              print 
    "Is Valid: " eregi('^[A-Z_0-9][A-Z_0-9.]*$'$file) . "<br>";
              exit(); 
          } 
      
          
    /* 
          ** //check that the user has permission to download file 
          ** if(user does not have permission) 
          ** { 
          **     //redirect to error page 
          **     header("Location: error.php"); 
          **     exit(); 
          ** } 
          */ 
      
          
    $mimetype = array( 
              
    'doc'=>'application/msword'
              
    'htm'=>'text/html'
              
    'html'=>'text/html'
              
    'jpg'=>'image/jpeg'
              
    'pdf'=>'application/pdf'
              
    'txt'=>'text/plain'
              
    'xls'=>'application/vnd.ms-excel',
              
    'mp3'=>'audio/mpeg'
              
    ); 
      
          
    $p explode('.'$file); 
          
    $pc count($p); 
      
          
    //send headers 
          
    if(($pc 1) AND isset($mimetype[$p[$pc 1]])) 
          { 
              
    //display file inside browser 
              
    header("Content-type: " $mimetype[$p[$pc 1]] . "\n"); 
          } 
          else 
          { 
              
    //force download dialog 
              
    header("Content-type: application/octet-stream\n"); 
              
    header("Content-disposition: attachment; filename=\"$file\"\n"); 
          } 
          
    header("Content-transfer-encoding: binary\n"); 
          
    header("Content-length: " filesize($path) . "\n"); 
      
          
    //send file contents 
          
    $fp=fopen($path"r"); 
          
    fpassthru($fp); 
        }
    ?>

    thanks,

    squirrelly1
    Last edited by squirrelly1; May 18th, 2005 at 09:13 AM.
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: ignore mime types

    You just need to send an extra header to force it to download:
    PHP Code:
    header('Content-Disposition: attachment; filename="nameoffile.ext"'); 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Re: ignore mime types

    Yes... I had that

    PHP Code:
              //force download dialog 
              
    header("Content-type: application/octet-stream\n"); 
              
    header("Content-disposition: attachment; filename=\"$file\"\n"); 
    but what I didn't have taken care of was the default player wanting to start up inside of the browser (quicktime pluggin)

    so I needed to have another mime type in there for the audio files that was something like this

    application/force-download

    so that the browser wouldn't try to play it automatically...


    Thanks,

    squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: ignore mime types

    If I am not wrong you have the '.mp3' file extension in your array. Therefore it will play in the browser. To have it download simply remove the mp3 extension from your array.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: ignore mime types

    By the way, "readfile" is probably faster than "fopen+fpassthru".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Re: ignore mime types

    I got it fixed with the thing I posted last time...

    When I removed the .mp3 from the array, it would still include the file as part of the webpage and would have me download the whole thing (to include the html), which of course wouldn't work.

    Thanks,

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

Posting Permissions

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



Click Here to Expand Forum to Full Width