在PHP中包含PHP文件 [英] Including a PHP file within PHP

查看:82
本文介绍了在PHP中包含PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行PHP if语句的网站,根据附加的文件类型显示内容,即Jpg,Txt,MP4。

I have a website which is running a PHP if statement to show content based on the type of file attached, i.e. Jpg, Txt, MP4.

所以我的代码用于显示TXT文件的是:

So my code for showing TXT files is:

if($post_attachment == 'txt'){$display_attachment = "

        <div class=\"sectionDivide\"></div>
        <div class=\"section\">
            <div class=\"articleAttachment\">
                include(\"../a/files/attachments/$post_year/$post_month/$post_id.$post_attachment\");
            </div>
        </div>

    ";}

问题是它没有显示TXT文件的内容,而是显示行 include(../ a / files / attachments / 2016/11 / 161123095119.txt); 在我的网页中。

The problem is that it's not displaying the content of the TXT file, but instead showing the line include("../a/files/attachments/2016/11/161123095119.txt"); in my web page.

如何让它显示我回显的文本文件的内容 $ display_attachment

How can I get it to display the content of the text file where I echo out $display_attachment?

推荐答案

直接(和原始)方式是使用php的字符串连接运算符():

The immediate (and crude) way is to use php's string concatenation operator (.):

$display_attachment = sprintf('
        <div class="sectionDivide"></div>
        <div class="section">
            <div class="articleAttachment">' . 
    include("/some/path/to/file") . '
            </div>
        </div>
');






稍微不那么混乱,但仍然不是真的可读变体是使用 sprintf()

$display_attachment = sprintf('
        <div class="sectionDivide"></div>
        <div class="section">
            <div class="articleAttachment">
%s
            </div>
        </div>
',
    include("/some/path/to/file")
);






一个干净的解决方案可以获取包含的结果到一个字符串变量并使用它。


A clean solution would fetch the result of the inclusion into a string variable and use that.

这篇关于在PHP中包含PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆