PHP: How do I display the contents of a textfile on my page?

前端 未结 8 732
囚心锁ツ
囚心锁ツ 2020-12-16 09:37

I have a .txt file on my web server (locally) and wish to display the contents within a page (stored on the same server) via PHP echo.

The .txt file contains a numbe

相关标签:
8条回答
  • 2020-12-16 10:07

    if you just want to show the file itself:

    header('Content-Type: text/plain');
    header('Content-Disposition: inline; filename="filename.txt"');
    readfile(path);
    
    0 讨论(0)
  • 2020-12-16 10:14

    I had to use nl2br to display the carriage returns correctly and it worked for me:

    <?php
    echo nl2br(file_get_contents( "filename.php" )); // get the contents, and echo it out.
    ?>
    
    0 讨论(0)
  • 2020-12-16 10:16

    If you aren't looking to do anything to the stuff in the file, just display it, you can actually just include() it. include works for any file type, but of course it runs any php code it finds inside.

    0 讨论(0)
  • 2020-12-16 10:17

    For just reading file and outputting it the best one would be readfile.

    0 讨论(0)
  • 2020-12-16 10:23

    I have to display files of computer code. If special characters are inside the file like less than or greater than, a simple "include" will not display them. Try:

    $file = 'code.ino';
    $orig = file_get_contents($file);
    $a = htmlentities($orig);
    
    echo '<code>';
    echo '<pre>';
    
    echo $a;
    
    echo '</pre>';
    echo '</code>';
    
    0 讨论(0)
  • 2020-12-16 10:23
    <?php
    $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
    echo fread($myfile,filesize("webdictionary.txt"));
    fclose($myfile);
    ?>
    

    Try this to open a file in php

    Refer this: (http://www.w3schools.com/php/showphp.asp?filename=demo_file_fopen)

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