如何在php.ini及PHP内置本地服务器配置text/markdown的Content-Disposition?
Great question! Let's walk through exactly how to make your Markdown files display inline in the browser instead of triggering downloads, plus cover the php.ini configuration options you're asking about.
Can we set Content-Disposition for text/markdown?
Absolutely! PHP's built-in server doesn't handle .md files out of the box with the right headers, but we have two solid approaches to fix this—one project-specific and flexible, the other global via php.ini.
Approach 1: Custom Router Script (Recommended for Flexibility)
The built-in server lets you specify a router script that intercepts all requests. This is perfect for targeting just your Markdown files without affecting other projects.
- Create a
router.phpfile in your project root:
<?php $requestPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $targetFile = __DIR__ . $requestPath; // Check if we're dealing with a Markdown file if (pathinfo($targetFile, PATHINFO_EXTENSION) === 'md') { // Set the correct MIME type for Markdown header('Content-Type: text/markdown'); // Force inline display instead of download header('Content-Disposition: inline; filename="' . basename($targetFile) . '"'); // Output the file content readfile($targetFile); exit; } // Let the server handle all other files normally return false;
- Start the server with your router:
php -S localhost:8000 router.php
Now when you visit http://localhost:8000/your-document.md, the browser will display the file inline (note: most browsers need a Markdown viewer extension to render it as formatted text instead of raw code).
Approach 2: Global Configuration via php.ini
If you want this behavior to apply to all instances of PHP's built-in server, you can use a global prepend script combined with MIME type setup.
- Create a global header script (e.g.,
markdown_headers.php):
<?php $currentFile = $_SERVER['SCRIPT_FILENAME']; if (pathinfo($currentFile, PATHINFO_EXTENSION) === 'md') { header('Content-Type: text/markdown'); header('Content-Disposition: inline'); }
- Update your php.ini:
Add this line to point to your script (replace the path with your actual file location):
auto_prepend_file = "/home/your-user/markdown_headers.php"
- Ensure
.mdmaps totext/markdown:
PHP uses amime.typesfile to detect MIME types. Locate your system'smime.types(common paths:/etc/mime.typesorphp_home/lib/mime.types) and add this line if it's missing:
text/markdown md
⚠️ Note: This global approach can conflict with other projects if they have different needs, so the router script is usually the better choice.
Quick Notes
- Browser support: Most browsers don't natively render Markdown as formatted HTML. Install an extension like Markdown Viewer to see styled content instead of raw text.
- Want HTML rendering directly? Add a Markdown parsing library like Parsedown to your router script—convert the
.mdcontent to HTML before outputting, and setContent-Type: text/htmlinstead.
内容的提问来源于stack exchange,提问作者kimbrys

