0

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 Answers 4

2
$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

Sign up to request clarification or add additional context in comments.

Comments

2

In completion of White Marcus answer

use the multiple="multiple"attribute in the input file element.

You should have : $_FILES variable, instead of $_POST

<?php

  var_dump($_FILES);

?>

To print a file use file_put_contents()

Best regards

2 Comments

Thanks but how can I save the contents of the files to a string?
Edit answer : use file_put_contents function
1
    $filenames = array();

    if(isset($_FILES["documents"]))
    {
       for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
       {
           $filenames[$i] = $_FILES['documents']['name'][$i];
        }

    print_r($filename);
    }

2 Comments

I tnk , Are u got it?
Would this print only the file name? I want to save the contents in each of the uploaded files to a string. @White Marcus
0

use the multiple="multiple"attribute in the input file element.

4 Comments

<input type="submit" value="Upload File" multiple="multiple"/> exactly this.
Ok say I did that. How do I loop through the files and save them to a string? @White Marcus
add another attribute name. <input name="documents[]" type="submit" value="Upload File" multiple="multiple"/> use this name attribute's documents array u can easily fetch the file names and store it into string using implode() method.
Thanks for that. Can you show me how to store the file contents into a string by giving me the code for it? @White Marcus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.