Results 1 to 6 of 6

Thread: [resolved] ignore mime types

Threaded View

  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?

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