[英]How can I let a user upload multiple text files and then loop through them and echo the contents of each one in PHP?
[英]How can I allow a user to upload multiple text files, then save them to a string and display them (in PHP)?
我想允許用戶使用簡單的HTML表單上傳多個文本文件,然后將它們保存為字符串。 之后,我想將它們顯示給用戶。 例如,假設我有三個文本文件,其內容如下:
file1.txt:這是一些文本。
file2.txt:這是更多文本。
file3.txt:更多文字!
當用戶使用HTML表單上傳它們時,我想將它們保存為字符串並像這樣顯示它們:
這是一些文字。
這是更多的文字。
甚至更多的文字!
我有以下(不完整的)代碼嘗試僅獲取一個文件:
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;
?>
如何遍歷文件並將其保存為字符串,然后以最佳方式顯示它們?
完成白色馬庫斯答案
在輸入文件元素中使用multiple =“ multiple”屬性。
您應該具有:$ _FILES變量,而不是$ _POST
<?php
var_dump($_FILES);
?>
要打印文件,請使用file_put_contents()
最好的祝福
$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);
}
}
我認為這對您有幫助!!!!!!!!! 1
$filenames = array();
if(isset($_FILES["documents"]))
{
for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
{
$filenames[$i] = $_FILES['documents']['name'][$i];
}
print_r($filename);
}
在輸入文件元素中使用multiple="multiple"
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.