optikalefx Posted July 6, 2010 Share Posted July 6, 2010 I've been able to serve files forever now, and finally had a request for docx. Well, i used the mime type i got from microsofts site application/vnd.openxmlformats-officedocument.wordprocessingml.document and it says corrupt file whenever its served. now, i've tried with many different files to make sure it wasn't just a bad test, its definetly how im serving it. Are there special headers? or has anyone ever been successful at this? It is actually serving the file correctly but only after 2 pop up errors and warnings. So the content is there, its just throwing some kind of error. Here is some code. and before you ask, no there isn't a problem with the decrypt or encrypt method, i've tried with those off as well. When decrypted, i can double click and open docx just fine, but when i try to server it, i get corrupt errors. function _serveFile($key) { ini_set("memory_limit","128M"); $file = $this->_data[$key]; if(isset($file) && !empty($file)) { $ext = end(explode(".", $file)); $allowed = array("doc","rtf","txt","pdf","xls","jpg","png","tiff","jpeg"); //if(in_array($ext,$allowed)) { if(1) { switch($ext) { case "txt": $type = "text/plain"; break; case "rtf": $type = "application/rtf"; break; case "doc": $type = "application/msword"; break; case "docx": $type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break; case "xls": $type = "application/vnd.ms-excel"; break; case "pdf": $type = "appliction/pdf"; break; case "png": $type = "image/png"; break; case "gif": $type = "image/gif"; break; case "jpg": case "jpeg": $type = "image/jpeg"; break; case "tiff": $type = "image/tiff"; break; default: $type = "application/octet-stream"; break; } $name = $this->_data['first_name']."_".$this->_data['last_name']."_".$key.".".$ext; $tmp = $this->_createDecryptedTmpFile(UPLOAD_DIR.DS."files".DS.$file); 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:application/force-download"); header("Content-Type:application/download"); header("Content-Description:File Transfer"); header("Content-type:".$type); header("Content-disposition: attachment; filename=".$name); header("Content-Length:".filesize($tmp)); echo file_get_contents($tmp); } else { echo "The file type $ext is not allowed"; } } } thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/206938-how-to-serve-docx-files/ Share on other sites More sharing options...
myrddinwylt Posted July 6, 2010 Share Posted July 6, 2010 This is what works for me. The problem is the "open" mime type exceeds the 60 character limit. case "docx": $type = "application/msword"; break; Quote Link to comment https://forums.phpfreaks.com/topic/206938-how-to-serve-docx-files/#findComment-1082169 Share on other sites More sharing options...
TapeGun007 Posted July 7, 2010 Share Posted July 7, 2010 I have a directory that's full of .mp3, .pdf, .docx and other files. I use this file to fix the type in the header so the correct download options come up. Works great for all my files, including the .docx Let me know if this helped. <?php $File_Name=$_GET['file']; $File_Type = substr($File_Name, -4); switch ($File_Type) { Case ".mp3": $ContentType = "audio/mpeg3"; $File_Path="Music_Library/"; $File_Location=$File_Path.$File_Name; break; Case ".avi": $ContentType = "video/avi"; $File_Path="Videos/"; $File_Location=$File_Path.$File_Name; break; Case (".doc" OR "docx"): $ContentType = "application/msword"; $File_Path="Music_Library/"; $File_Location=$File_Path.$File_Name; break; Case ".gif": $ContentType = "image/gif"; $File_Path="Pictures/"; $File_Location=$File_Path.$File_Name; break; Case (".jpg" OR "jpeg"): $ContentType = "image/jpeg"; $File_Path="Pictures/"; $File_Location=$File_Path.$File_Name; break; Case ".wav": $ContentType = "audio/wav"; $File_Path="Music_Library/"; $File_Location=$File_Path.$File_Name; break; Case ".m4a": $ContentType = "audio/m4a"; $File_Path="Music_Library/"; $File_Location=$File_Path.$File_Name; break; Case (".mpg" OR "mpeg"): $ContentType = "video/mpeg"; $File_Path="Music_Library/"; $File_Location=$File_Path.$File_Name; break; Case ".rtf": $ContentType = "application/rtf"; $File_Path="Music_Library/"; $File_Location=$File_Path.$File_Name; break; Case ".pdf": $ContentType = "application/pdf"; $File_Path="Music_Library/"; $File_Location=$File_Path.$File_Name; break; Case ".txt": $ContentType = "application/txt"; $File_Path="Music_Library/"; $File_Location=$File_Path.$File_Name; break; } @ob_end_clean(); // The following code makes the download non cacheable header("Cache-control: private"); header('Pragma: private'); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header('Content-disposition: attachment; filename='.$File_Name); header('Content-type:'.$ContentType); readfile($File_Location); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/206938-how-to-serve-docx-files/#findComment-1082223 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.