how do I assign content from readfile() into a variable?

后端 未结 2 521
孤城傲影
孤城傲影 2021-01-13 02:34

readfile() says it outputs the content but I want the content to be saved in a variable. How do I do that? I tried $content=readfile(\"file.txt\") but that doesn\'t come out

相关标签:
2条回答
  • 2021-01-13 03:21

    That is what file_get_contents is for:

    $data = file_get_contents("text.txt");
    echo htmlentities($data);
    

    If you must use readfile you can use output buffering to get the contents into a variable:

    ob_start();
    readfile("text.txt");
    $data = ob_get_clean();
    echo htmlentities($data);
    

    I don't see why you would avoid file_get_contents in this situation though.

    0 讨论(0)
  • 2021-01-13 03:29

    readfile() writes the contents of the file to a buffer. Use file_get_contents() instead.

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