WordPress中用于PDF加载的自定义脚本:无法加载PDF文档

发布于 2025-01-30 17:47:12 字数 2588 浏览 3 评论 0 原文

我需要的东西: 我需要一个PHP脚本,可以加载一些在特定目录中存储的PDF文件。 例如,如果我转到url (或 www.mydomainameweb.com/pdf?file=a.pdf url对我来说并不重要) 这应该加载PDF文件带有header()函数。

我所做的: 我制作了一个新的PHP脚本/templates/elegant/page-pdf.php(我的脚本) 我还使用URL“ PDF”创建了WordPress页面。因此,当我加载mydomainameweb.com/pdf时,这将加载page-pdf.php脚本。 除了WP中的此脚本外,一切都不错,没有加载PDF文件。在没有WP的另一个域中使用此脚本 - 效果很好。

我不知道为什么脚本返回错误“无法加载PDF文档”。在外部域中,使用相同的脚本效果很好。问题仅与WordPress集成。

page-pdf.php脚本内容:

// I hope that $file path is good, because when I set truly incorrect path it returns error that no such a file.
$name_file = 'name.pdf';
if (mime_content_type($file) == 'application/pdf')
  {

    define('WP_USE_THEMES', false); // These lines I add only in WP script file
    status_header(200); // These lines I add only in WP script file
    // Header content type
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $name_file . '"');
    header('Content-Transfer-Encoding: binary');
    header('Accept-Ranges: bytes');

    // Read the file
    @readfile($file);
  }

更新: 我现在尝试的是:

创建了典范的脚本,并试图直接打开它: www.mydomainpagename12345.com/wp-content/wp-content/themes/themes/eelegant/elelegant/test/test/test/test/test/test/test/test-.mynomainpagename12345.com/ php

我有一个错误: 警告:mime_content_type(wp-content/themes/Elegent/all_files/this-dir/file.pdf):无法打开流:/home/myprojects/mydomains/mydomainpagename12345.com/public_html/wp-contml/wp-contml/wp-contml/wp-contml/wp-contml/wp-contml/wp-contml/wp-contml/wp-contml/wp-contml/wp-con-home/myproject一下/themes/elegant/test.php在第37行上,

但是当我直接打开PDF时,我可以看到该文件: mydomainpagename12345.com/wp-content/themes/elegent/all_files/this-dir/file.pdf

更新编号2: 我尝试使用PHP脚本存储并仅将一个文件设置在同一目录中: $ file ='a.pdf'; 而且很棒。这个脚本有效。但是问题是,现在我没有直接获得$ _Session数据打开test.php脚本。为什么我会直接丢失会话数据打开PHP文件? 丢失$ _SESSION ['my_session']为什么?该怎么办?

What I need:
I need a PHP script, that can load some PDF files stored in a specific directory.
For example if I go to URL www.mydomainameweb.com/wp-content/themes/elegant/pdf.php?file=a.pdf (or www.mydomainameweb.com/pdf?file=a.pdf URL for me doesn't matter so much)
This should load the PDF file with header() function.

What I did:
I made a new PHP script /templates/elegant/page-pdf.php (my script)
And I also created WordPress page with URL “pdf”. So when I am loading mydomainameweb.com/pdf, this loads page-pdf.php script.
All good except this script in WP doesn’t load PDF file. Using this script in another domain without WP – works great.

I don't know why script returns error "Failed to load PDF document". While in external domain using the same script works great. Problem is only integrating with WordPress.

page-pdf.php script content:

// I hope that $file path is good, because when I set truly incorrect path it returns error that no such a file.
$name_file = 'name.pdf';
if (mime_content_type($file) == 'application/pdf')
  {

    define('WP_USE_THEMES', false); // These lines I add only in WP script file
    status_header(200); // These lines I add only in WP script file
    // Header content type
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $name_file . '"');
    header('Content-Transfer-Encoding: binary');
    header('Accept-Ranges: bytes');

    // Read the file
    @readfile($file);
  }

UPDATE:
What I tried now:

Created dublicated script and tried to open it directly:
www.mydomainpagename12345.com/wp-content/themes/elegant/test.php

I got this error:
Warning: mime_content_type(wp-content/themes/elegent/all_files/this-dir/file.pdf): failed to open stream: No such file or directory in /home/myprojects/domains/mydomainpagename12345.com/public_html/wp-content/themes/elegant/test.php on line 37

But when I am opening directly the PDF, I can see the file:
mydomainpagename12345.com/wp-content/themes/elegent/all_files/this-dir/file.pdf

UPDATE number 2:
I tried to store and set only one file in the same directory with PHP script:
$file = 'a.pdf';
And great. This script works. But the problem is that now I didn't get $_SESSION data opening test.php script directly. Why did I lost session data opening PHP file directly?
www.mydomainameweb.com/wp-content/themes/elegant/test.php?file=a.pdf Loses $_SESSION['my_session'] WHY? What to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2025-02-06 17:47:13

我花了2-3天解决这个问题。
我必须加载外部PHP。没问题。问题是外部脚本不会从WordPress获得会话。在这个问题上花了几天的时间为我提供了一个非常简单的解决方案。在外部(同一域,但在WP结构中不在)的开头,您也需要添加两件事:

  1. load wp-load.php文件。这允许使用WP设置和会话。
  2. 如果此文件在模板目录内,则添加模板注释会话。在下面的一开始,评论并不完整。您应该根据模板使用评论部分。

没有人帮助我。但是,如果将来有人需要,我将留下这个解决方案。

解决方案:

<?php
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
/**
 * The template for displaying all pages.
 *
.....

I spent 2-3 days with this issue.
I had to load external PHP. It was no problem. Problem was that external script doesn't get SESSION from WordPress. Spending few days with this issue got me a very simple solution. In the beginning of the external (same domain, but not in WP structure) PHP file you need too add two things:

  1. Load wp-load.php file. That allows to work with WP settings and SESSIONS.
  2. Add template comment session if this file is inside the template directory. In the beginning below, comment is not full. You should use your comment section according to your template.

Nobody helped me. But I will leave this solution if anybody needs it in the future.

SOLUTION:

<?php
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
/**
 * The template for displaying all pages.
 *
.....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文