无法更正文件的回显内容

时间:2018-03-10 17:00:11

标签: php echo

我对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=""/>

有没有人有任何可以提供帮助的线索或见解?我检查了类似的问题,但没有给我一个答案。我知道//之前的回声'这是用于测试。

亲切的问候

1 个答案:

答案 0 :(得分:0)

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

要阅读文件,而不是你拥有的各种循环,你应该......

$string = file_get_contents($file);

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