Read a .php file using php

后端 未结 3 1760
暗喜
暗喜 2021-01-14 18:01

How to read a .php file using php

相关标签:
3条回答
  • 2021-01-14 18:40

    Let's say you have two files a.php and b.php on same folder.

    Code on the file b.php

    <?php
    
    echo "hi";
    
    ?>
    

    and code on a.php

    <?php
    $data = file_get_contents('b.php');
    echo $data;
    

    You access a.php on browser.

    What do you see? A blank page.

    Please check the page source now. It is there.

    But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.

    <?php
    $data = htmlentities(file_get_contents('b.php'));
    echo $data;
    

    Now you can see the output in browser.

    0 讨论(0)
  • 2021-01-14 18:53

    If you want to get the content generated by PHP, then

    $data = file_get_contents('http://host/path/file.php');
    

    If you want to get the source code of the PHP file, then

    $data = file_get_contents('path/file.php');
    

    Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.

    0 讨论(0)
  • 2021-01-14 18:54
    //get the real path of the file in folder if necessary
    $path = realpath("/path/to/myfilename.php");
    
    //read the file
    $lines = file($path,FILE_IGNORE_NEW_LINES);
    

    Each line of the 'myfilename.php' will be stored as a string in the array '$lines'. And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php

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