Why readfile() is acting strange in reading a php file but working fine on text file

前端 未结 2 1920
北荒
北荒 2021-01-23 20:58

Code in index.php


code in hello.php



        
相关标签:
2条回答
  • 2021-01-23 21:06

    This is happening because readfile echos to the screen for you. echo readfile() is unnecessary. After echoing, readfile returns the number of bytes that it read, so that's where the 47 is coming from.

    You can just do:

    readfile('hello.php');
    

    or you can use

    echo file_get_contents('hello.php');
    

    if you choose.

    Also, I suggest you check the source of your page in your browser (consult your browser's docs). Chances are, you'll see the PHP file clearly there. Your browser is probably trying to render the response as an HTML file. It's trying to parse <?php as an HTML tag. That's why it's not displayed.

    To get around this, you can try to add

    header('Content-type: text/plain');
    

    before you echo your hello.php file to the screen. This should tell the browser that it's receiving just a text file and not to try to parse it as HTML.

    0 讨论(0)
  • 2021-01-23 21:06

    The behavior for php file is same as other files. If you go to page source you will see the <?php tag is also present.

    enter image description here

    0 讨论(0)
提交回复
热议问题