无法更正文件的回显内容

Can not correct echo content of file

我是 PHP 的新手,很难将纯文本文件回显到某个区域。

$string = "";

if(isset($_POST['submitText']))
{
    if($_FILES){
        if($_FILES['file']['name'] != "") {
            if(isset($_FILES) && $_FILES['file']['type'] != 'text/plain') {
                echo "<span>File could not be accepted ! Please upload any '*.txt' file.</span>";
                exit();
            } 

            // echo "<center><span id='Content'>Contents of ".$_FILES['file']['name']." File</span></center>";

            $fileName = $_FILES['file']['tmp_name'];

            $file = fopen($fileName,"r") or exit("Unable to open file!");

            while(!feof($file)) {
            echo fgets($file). "";
            }

            while(!feof($file)) {
            $string = fgetc($file);
            // echo $string;
            }
       fclose($file);
       }
    }
}

<textarea name="string" value="" rows="10" columns="90" placeholder="Paste your text here or upload" id="" style="" ><?php echo $string; ?>
</textarea> 
<input type="file" name="file" size="60" style="" onchange="myFunction()"/>
<input type="submit" name="submitText" value="Read Contents" style=""/>

有没有人有任何线索或见解可以提供帮助?我检查过类似的问题,但没有真正给我答案。我知道 // 在 echo 之前这是为了测试。

亲切的问候

第一个循环应该读取所​​有文件并只回显内容,所以当第二个循环尝试读取文件时,它根本不会 运行 而 $string 总是是空的。

要读取一个文件,而不是你拥有的各种循环,你应该做...

$string = file_get_contents($file);

这将替换 fopen() 和两个循环中的所有代码。