Irap Posted March 26, 2010 Share Posted March 26, 2010 The file being outputted is a .rar # $file['filename'] = "myfile.rar"; // given by database $file['extension'] = substr( $file['filename'], -3 ); // Set the mime according to the extension. // I believe these are appropriate mime types to use. if ( $file['extension'] == "rar" ) { $file['mime'] = "application/x-rar-compressed"; } elseif ( $file['extension'] == "zip" ) { $file['mime'] = "application/zip"; } // Added the if statement just in case the above wasn't working. if ( $file['mime'] == "application/zip" || $file['mime'] == "application/x-rar-compressed" ) { mysql_query(" UPDATE `web_downloads_mods` SET `downloads` ='{$file_mod['file_downloads']}' WHERE `id` ='{$file['id']}' LIMIT 1 "); @header( "Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT" ); # expire the file in 2 hours. @header( "Last-Modified: " . gmdate("D, d M Y H:i:s", $file['uploaded']) . " GMT" ); @header( "Content-Length: {$file['filesize']}" ); # given by the database in bytes. @header( "Content-Disposition: attachment; filename='". $file['filename'] ."'" ); // Internet Explorer compatibility: @header( "Pragma: " ); @header( "Cache-Control: " ); readfile( "http://". $_SERVER['HTTP_HOST'] ."/uploads/". $file['filename'] ); } Firefox will open the file in a new tab and display gibberish... Except, of course... At the beginning of the file there is a fun "Rar!". Is this something that occurs in many files and can be used to test if files are the right extension? Quote Link to comment https://forums.phpfreaks.com/topic/196608-wrong-mimetype-output-html-doc-instead-of-rar-or-zip/ Share on other sites More sharing options...
Kieran Menor Posted March 26, 2010 Share Posted March 26, 2010 You need to specify a Content-Type header. @header("Content-Type: {$file['mime']}"); Edit: On that note, why are you suppressing errors from the header() calls? They won't output any errors unless you already sent content data to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/196608-wrong-mimetype-output-html-doc-instead-of-rar-or-zip/#findComment-1032265 Share on other sites More sharing options...
Irap Posted March 26, 2010 Author Share Posted March 26, 2010 Heh, I thought I had used content type. The problem with PHP outputting errors is that it messes up the file that is being printed. As they say, don't output anything else besides from the file data when using header() to output files... Otherwise you get an error at the start of the file. Quote Link to comment https://forums.phpfreaks.com/topic/196608-wrong-mimetype-output-html-doc-instead-of-rar-or-zip/#findComment-1032271 Share on other sites More sharing options...
Kieran Menor Posted March 26, 2010 Share Posted March 26, 2010 The problem is that if the header() calls fail, the whole deal will mess up, so there is no point in suppressing errors. You just have to make sure they never fail. Quote Link to comment https://forums.phpfreaks.com/topic/196608-wrong-mimetype-output-html-doc-instead-of-rar-or-zip/#findComment-1032273 Share on other sites More sharing options...
Irap Posted March 26, 2010 Author Share Posted March 26, 2010 I prefer to have it there as a "just in case" measure. There has also been times when PHP Notices have appeared in the file and there's not much to bother doing about that. Thanks for the help, anyways. Quote Link to comment https://forums.phpfreaks.com/topic/196608-wrong-mimetype-output-html-doc-instead-of-rar-or-zip/#findComment-1032288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.