Wednesday, October 23, 2013

php, play mp3 file outside web server directory



To prevent that mp3 audio files are downloaded, we may need to  put mp3  file outside  web server directory. On the other hand, we also want to play these mp3 files online. The following PHP script is used to play the nn.mp3 file in folder C:\LMS\uploads\, which is not related to the web server folder.
<?php
$file = "C:\\LMS\\uploads\\nn.mp3";
$real = realpath($file);
if (file_exists($real)){

     $ext   = 'audio/mpeg';
      $size  = filesize($real);
      $size2 = $size-1;
      header('Content-Type: '.$ext);
      header("Content-Range: bytes 0-$size2/$size");
      header('Content-Length: '.$size);
      echo @file_get_contents($real);

}else {
       echo $file." does not exsit";

}
?>

For other file formats, $ext will be changed as below:
$mime['zip']   = 'application/zip';
$mime['au']    = 'audio/basic';
$mime['snd']   = 'audio/basic';
$mime['mid']   = 'audio/midi';
$mime['midi']  = 'audio/midi';
$mime['kar']   = 'audio/midi';
$mime['mpga']  = 'audio/mpeg';
$mime['mp2']   = 'audio/mpeg';
$mime['mp3']   = 'audio/mpeg';
$mime['aif']   = 'audio/x-aiff';
$mime['aiff']  = 'audio/x-aiff';
$mime['aifc']  = 'audio/x-aiff';
$mime['ram']   = 'audio/x-pn-realaudio';
$mime['rm']    = 'audio/x-pn-realaudio';
$mime['ra']    = 'audio/x-realaudio';
$mime['wav']   = 'audio/x-wav';
$mime['pdb']   = 'chemical/x-pdb';
$mime['xyz']   = 'chemical/x-pdb';
$mime['gif']   = 'image/gif';
$mime['ief']   = 'image/ief';
$mime['jpeg']  = 'image/jpeg';
$mime['jpg']   = 'image/jpeg';
$mime['jpe']   = 'image/jpeg';
$mime['png']   = 'image/png';
$mime['tiff']  = 'image/tiff';
$mime['tif']   = 'image/tiff';

$mime['xml']   = 'text/xml';
$mime['mpeg']  = 'video/mpeg';
$mime['mpg']   = 'video/mpeg';
$mime['mpe']   = 'video/mpeg';
$mime['qt']    = 'video/quicktime';
$mime['mov']   = 'video/quicktime';
$mime['avi']   = 'video/x-msvideo';
$mime['movie'] = 'video/x-sgi-movie';
$mime['ice']   = 'x-conference/x-cooltalk';
$mime['html']  = 'text/html';
$mime['htm']   = 'text/html';
$mime['xls']   = 'application/vnd.ms-excel';
$mime['log']   = 'text/plain';

$mime['gtar']  = 'application/x-gtar';
$mime['gz']    = 'application/x-gzip';
$mime['tgz']   = 'application/x-gzip';

$mime['pdf']   = 'application/pdf';
$mime['swf']   = 'application/x-shockwave-flash';

No comments:

Post a Comment