garrisonian14 Posted December 4, 2007 Share Posted December 4, 2007 hi, I want to download a text file using php, Can any body please tell me the code. i am using following $file_path = "terms.txt"; $path_parts = pathinfo($file_path); $fext = $path_parts['extension']; $asfname = $path_parts['basename']; set_time_limit(0); //echo "path".$file_path; if(!file_exists($file_path)) { die("File does not exist!"); } // file size in bytes $fsize = filesize($file_path); $mtype = ''; // mime type is not set, get from server settings if (function_exists('mime_content_type')) { $mtype = mime_content_type($file_path); } else if (function_exists('finfo_file')) { $finfo = finfo_open(FILEINFO_MIME); // return mime type $mtype = finfo_file($finfo, $file_path); finfo_close($finfo); } if ($mtype == '') { if($fext == "pdf") { $mtype = 'application/pdf'; } else if($fext == "txt" || $fext == "htm" || $fext == "html") { $mtype = 'text/html'; } else { $mtype = "application/force-download"; } } // set headers header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: $mtype"); header("Content-Disposition: attachment; filename=\"$asfname\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . $fsize); @readfile($file_path); But instead of downloading the file i am specifying , i downloads source code of the page where this download code is written. Kindly help me as soon as possible. regards, Ali. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 4, 2007 Share Posted December 4, 2007 change header("Content-Type: $mtype"); to header("Content-type:application/octetstream"); Quote Link to comment 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.