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?
$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
use the multiple="multiple"
attribute in the input file element.
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
$filenames = array();
if(isset($_FILES["documents"]))
{
for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
{
$filenames[$i] = $_FILES['documents']['name'][$i];
}
print_r($filename);
}