Jump to content

apache_lookup_uri for determining mime-type


jon23d

Recommended Posts

I've come to the conclusion that I hate mod_rewrite, so I'd just like to pass everything off to php.  I also don't want to have to rely on file extensions for mime type, or deal with the same problems everyone else is having with the php functions to do so.  Is there any reason I couldn't just do this? :

 

/**
	 * Serve the provided file to the browser
	 * 
	 * Headers cannot have been sent
	 * 
	 * Returns FALSE if headers have been sent,
	 * if the file does not exist or is innacessible,
	 * or if it is a folder.
	 * 
	 * If $force_save_dialog is true, then the browser
	 * will show a save dialog instead of rendering the contents
	 * 
	 * @static 
	 *
	 * @param STRING $filename_with_path
	 * @param BOOL $force_save_dialog
	 * @return BOOL
	 */
	static function serveFile(STRING $filename_with_path, BOOL $force_save_dialog = false) {
		// exists?  headers sent?
		if (!is_file($filename_with_path) || headers_sent()) return false;
		// get mime-type, test for readibility
		$file_info = apache_lookup_uri($filename_with_path);
		if (!$file_info->filename) return false;
		$filename = $file_info->filename;
		// send the mime-type
		header('Content-type: ' . $file_info -> content_type);
		// force save if requested
		if ($force_save_dialog) header("Content-Disposition: attachment; filename=\"$filename\"");
		// output file
		readfile($filename_with_path);
		return true;
	}

 

I understand that caching the results of apache_lookup_uri could offer a performance improvement.

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.