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

PHP实现点击链接调用Word/PDF阅读器打开本地文档的方法

First off, it’s important to clarify: PHP runs on your server, so it can’t directly launch apps like Microsoft Word or a PDF reader on the user’s computer. What we can do is configure how the file is served to the browser, so it prompts the user to open the file with their default associated app for that file type. Here’s how to fix your current setup:

Step 1: Create a PHP Script to Serve Files with Correct Headers

This script will handle reading your local files and sending the right HTTP headers to tell the browser how to handle the file (e.g., open it with Word or a PDF reader instead of just downloading it).

Create a new file named open_file.php with this code:

<?php
// Define the directory where your .doc/.pdf files are stored
$allowedDirectory = __DIR__ . '/temp/es/';

// Sanitize the requested filename to prevent security risks (like directory traversal)
$requestedFile = basename($_GET['file']);
$fullFilePath = $allowedDirectory . $requestedFile;

// Validate the file exists and is within the allowed directory
if (!file_exists($fullFilePath) || strpos(realpath($fullFilePath), realpath($allowedDirectory)) !== 0) {
    http_response_code(404);
    echo "File not found or access denied.";
    exit;
}

// Set the correct content type based on file extension
$fileExtension = strtolower(pathinfo($requestedFile, PATHINFO_EXTENSION));
switch ($fileExtension) {
    case 'doc':
        $contentType = 'application/msword';
        break;
    case 'pdf':
        $contentType = 'application/pdf';
        break;
    default:
        http_response_code(400);
        echo "Unsupported file type.";
        exit;
}

// Send headers to prompt the browser to open the file with the default app
header('Content-Type: ' . $contentType);
header('Content-Disposition: inline; filename="' . $requestedFile . '"');
header('Content-Length: ' . filesize($fullFilePath));

// Output the file content
readfile($fullFilePath);
exit;
?>

Instead of linking directly to the file, point your <a> tags to the open_file.php script and pass the filename as a parameter.

Your original code:

echo "<a href=\"temp/es/doc1.pdf\">Open</a>";

Updated code:

echo "<a href=\"open_file.php?file=doc1.pdf\">Open PDF</a>";
echo "<a href=\"open_file.php?file=doc1.doc\">Open Word Document</a>";

Key Notes to Keep in Mind

  • Security: The script uses basename() and checks the real file path to block malicious requests (like someone trying to access files outside your temp/es/ directory). Never skip this sanitization step!
  • Browser Behavior: The final decision of which app opens the file is up to the user’s browser and system settings. For example, some browsers might open PDFs in their built-in viewer unless the user has set a different default app.
  • Force Download: If you want to prompt a download instead of opening the file directly, change Content-Disposition: inline to Content-Disposition: attachment in the script.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:04:05