我需要的东西:
我需要一个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?
发布评论
评论(1)
我花了2-3天解决这个问题。
我必须加载外部PHP。没问题。问题是外部脚本不会从WordPress获得会话。在这个问题上花了几天的时间为我提供了一个非常简单的解决方案。在外部(同一域,但在WP结构中不在)的开头,您也需要添加两件事:
没有人帮助我。但是,如果将来有人需要,我将留下这个解决方案。
解决方案:
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:
Nobody helped me. But I will leave this solution if anybody needs it in the future.
SOLUTION: