simple way to display data in a .txt file on a webpage?

后端 未结 8 1014
遥遥无期
遥遥无期 2020-12-13 02:23

Working on a project, one of the webpages will display a list of people (specifically, a list of people from a graduation class that haven\'t been located yet). Instead of m

相关标签:
8条回答
  • 2020-12-13 02:43

    That's the code I use:

    <?php   
        $path="C:/foopath/";
        $file="foofile.txt";
    
        //read file contents
        $content="
            <h2>$file</h2>
                <code>
                    <pre>".htmlspecialchars(file_get_contents("$path/$file"))."</pre>
                </code>";
    
        //display
        echo $content;
    ?>
    

    Keep in mind that if the user can modify $path or $file (for example via $_GET or $_POST), he/she will be able to see all your source files (danger!)

    0 讨论(0)
  • 2020-12-13 02:44

    Easy way:

    • Rename missingmen.txt to missingmen.html.
    • Add a single line to the top of missingmen.html:
      <link href="txtstyle.css" rel="stylesheet" type="text/css" />
    • Create a file called txtstyle.css, and add to it a line like this:
      html, body {font-family:Helvetica, Arial, sans-serif}
    0 讨论(0)
  • 2020-12-13 02:47

    In more recent browsers code like below may be enough.

    <object data="https://www.w3.org/TR/PNG/iso_8859-1.txt" width="300" height="200">
    Not supported
    </object>

    0 讨论(0)
  • 2020-12-13 02:47

    You can add it as script file. save the txt file with js suffix

    in the head section add

    <script src="fileName.js"></script>

    0 讨论(0)
  • 2020-12-13 02:56

    You cannot style a text file, it must be HTML

    0 讨论(0)
  • 2020-12-13 02:59

    If you just want to throw the contents of the file onto the screen you can try using PHP.

    <?php
        $myfilename = "mytextfile.txt";
        if(file_exists($myfilename)){
          echo file_get_contents($myfilename);
        }
    ?>
    

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