dongtigai3875 2014-12-04 10:50
浏览 74
已采纳

如何允许用户上传多个文本文件,然后将它们保存为字符串并显示它们(在PHP中)?

I want to allow a user to upload multiple text files using a simple HTML form, and then save them to a string. After that, I want to display them to the user. For example, say I have three text files with the following contents:

file1.txt: this is some text.

file2.txt: this is some more text.

file3.txt: even more text!

When the user uploads them using an HTML form, I want to save them to a string and display them like so:

this is some text.

this is some more text.

even more text!

I have the following (incomplete) code that attempts to get only one file:

    Upload text documents: <br /><br>
    <form action="output.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" size="50" />
    <br />
    <input type="submit" value="Upload File" />
    </form>

    <?php
    $doc = $_POST['file'];
    echo $doc;
    ?>

How can I loop through the files and save them to a string, then display them in the best way possible?

  • 写回答

4条回答 默认 最新

  • doujiayao8433 2014-12-04 11:18
    关注
    $filenames = array();
    
        if(isset($_FILES["documents"]))
        {
           for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
           {
               $filenames[$i] = $_FILES['documents']['name'][$i];
    
               $myfile = fopen("$_FILES['documents']['name'][$i]", "r") or die("Unable to open file!");
    
               echo fread($myfile,filesize("$_FILES['documents']['name'][$i]"));
    
               fclose($myfile);
    
            }
    
        }
    

    I think this is helps for you!!!!!!!1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?