Jump to content

How to serve DOCX files?


optikalefx

Recommended Posts

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!!!

 

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.