@Peter,Thanks...lemme try it out.
You have two echo's before you start calling the header functions, this is generally a bad idea since the PHP manual says "...Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. "
Rewriting your code as below, i managed to make the file download ok.
<code>
<?php
$filepath="music/test.mp3";
$file_extension = strtolower(substr(strrchr($filepath,"."),1));
$filename = realpath($filepath);$ctype = getContentType($file_extension);
$file = @fopen($filename,"rb");
//set headersfunction getContentType($file_extension){
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".$filepath. "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$filename") or die("File not found.");
?>
switch ($file_extension) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "rar": $ctype="application/x-rar-compressed"; 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;
case 'mp3': $ctype='audio/mpeg'; break;
case 'mpg': $ctype='video/mpeg'; break;
case 'avi': $ctype='video/x-msvideo'; break;
default: $ctype='application/force-download';
}
}
</code>On Sun, Jul 11, 2010 at 4:16 PM, Musya mike <mmycool@gmail.com> wrote:
_______________________________________________Someone please help me on this...I want to make a user download music from my server...however this script displays gibberish stuff on the browser...how do i make it download?Ignore the header warnings...<?php//ob_start();//session_start();$filepath="./cool/scripts/files/music/01.mp3";$file_extension = strtolower(substr(strrchr($filepath,"."),1));echo $file_extension;$filename = realpath($filepath);$file = @fopen($filename,"rb");echo $filename;switch ($file_extension) {case "pdf": $ctype="application/pdf"; break;case "exe": $ctype="application/octet-stream"; break;case "zip": $ctype="application/zip"; break;case "rar": $ctype="application/x-rar-compressed"; 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;case 'mp3': $ctype='audio/mpeg'; break;case 'mpg': $ctype='video/mpeg'; break;case 'avi': $ctype='video/x-msvideo'; break;default: $ctype='application/force-download';}// set headersheader("Pragma: public");header("Expires: 0");header("Cache-Control: must-revalidate, post-check=0, pre-check=0");header("Cache-Control: private",false);header("Content-Type: $ctype");header("Content-Disposition: attachment; filename=\"".$filepath. "\";");header("Content-Transfer-Encoding: binary");header("Content-Length: ".@filesize($filename));set_time_limit(0);@readfile("$filename") or die("File not found.");/*while(!feof($file)) {print(fread($file, 1024*8));flush();if (connection_status()!=0) {@fclose($file);die();}}@fclose($file); */// @readfile("$filename") or die("File not found.");// print getenv("SCRIPT_NAME");?>
Skunkworks mailing list
Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
------------
Skunkworks Server donations spreadsheet
http://spreadsheets.google.com/ccc?key=0AopdHkqSqKL-dHlQVTMxU1VBdU1BSWJxdy1fbjAwOUE&hl=en
------------
Skunkworks Rules
http://my.co.ke/phpbb/viewtopic.php?f=24&t=94
------------
Other services @ http://my.co.ke
_______________________________________________
Skunkworks mailing list
Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
------------
Skunkworks Server donations spreadsheet
http://spreadsheets.google.com/ccc?key=0AopdHkqSqKL-dHlQVTMxU1VBdU1BSWJxdy1fbjAwOUE&hl=en
------------
Skunkworks Rules
http://my.co.ke/phpbb/viewtopic.php?f=24&t=94
------------
Other services @ http://my.co.ke