You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在php.ini及PHP内置本地服务器配置text/markdown的Content-Disposition?

Configuring PHP's Built-in Web Server for Inline Markdown Display

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.

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.

  1. Create a router.php file 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;
  1. 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.

  1. 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');
}
  1. 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"
  1. Ensure .md maps to text/markdown:
    PHP uses a mime.types file to detect MIME types. Locate your system's mime.types (common paths: /etc/mime.types or php_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 .md content to HTML before outputting, and set Content-Type: text/html instead.

内容的提问来源于stack exchange,提问作者kimbrys

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:12:47