简体   繁体   English

如何读取html文件中的文本文件并将内容复制到使用php的变量中

[英]how to read a text file inside html file and copy the contents into a variable using php

I'm new to HTML and when I searched for various ways of reading a text file inside HTML file, I found this code : 我是HTML的新手,当我搜索以多种方式读取HTML文件中的文本文件时,发现了以下代码:

<!DOCTYPE html>
<html>
    <head>
      <meta charset="UTF-8">
      <title>Page Title</title>
    </head>
<body>
<?php
    $myfile = fopen("C:\Users\nuh\Documents\webdictionary.txt", "r") or die("Unable to open file!");
    echo fgets($myfile);
    fclose($myfile);
?>
</body>
</html>

The PHP code does not run inside HTML. PHP代码不在HTML内运行。

Using javascript, I could not find a code which reads each line into a variable. 使用javascript,我找不到将每一行读入变量的代码。 Please suggest me another way to read the contents into a variable. 请建议我将内容读入变量的另一种方法。

For security purpose, javascript and HTML can't access the user filesystem. 为了安全起见,javascript和HTML无法访问用户文件系统。

If the file you try to read is on the server you can simply use readfile("C:\\Users\\nuh\\Documents\\webdictionary.txt"); 如果您尝试读取的文件在服务器上,则只需使用readfile("C:\\Users\\nuh\\Documents\\webdictionary.txt");

If the file is on the client side then there is no way you can access it from PHP directly. 如果文件在客户端,则无法从PHP直接访问它。 You have to use a form with file input and submit it to your PHP. 您必须使用带有文件输入的表单并将其提交到PHP。 This way the user will have to click and to choose the file and you will have to manage the content of the request. 这样,用户将不得不单击并选择文件,而您将必须管理请求的内容。

I don't know about any 3rd party plugin to do it. 我不知道有任何第三方插件可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM