如何在 PHP 中上传 .txt 文件并让它在另一页上逐行读取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10694050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does one upload a .txt file in PHP and have it read line by line on another page?
提问by user1409008
My objective here is to upload a .txt file on a form (browse), post the file to another php page, and then have that file read line by line.
我的目标是在表单(浏览)上上传 .txt 文件,将文件发布到另一个 php 页面,然后逐行读取该文件。
My code so far is here. FILE 1: HTML UPLOAD:
到目前为止,我的代码在这里。文件 1:HTML 上传:
<form action="TestParse.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label> <input type="file" name="file" id="file"/>
<input type="submit" value="Submit">
</form>
FILE 2: READING THE FILE
文件 2:阅读文件
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
elseif ($_FILES["file"]["type"] !== "text/plain")
{
echo "File must be a .txt";
}
else
{
$file_handle = fopen($_FILES["file"]["name"], "rb");
}
As I see it, the second file would verify that there is no error and that the uploaded file is a .txt. It would then fopen() the file and I would then be able to read with fgets() (I have managed to get all this to work).
在我看来,第二个文件将验证没有错误并且上传的文件是 .txt。然后它会 fopen() 文件,然后我就可以使用 fgets() 读取(我已经设法让所有这些工作)。
However, this code only works if the .txt file that is being uploaded happens to be in the same directory as the PHP file. Otherwise I get lots of error messages. And when you cannot upload a file that's not in the PHP file's folder, it defeats the purpose of having a file upload system in the first place.
但是,此代码仅在上传的 .txt 文件恰好与 PHP 文件位于同一目录中时才有效。否则我会收到很多错误消息。当您无法上传不在 PHP 文件文件夹中的文件时,首先就违背了拥有文件上传系统的目的。
Can someone tell me what is wrong with this code?
有人能告诉我这段代码有什么问题吗?
回答by ruben
to read line by line, it worked for me this code:
逐行阅读,它对我有用,这段代码:
$fp = fopen($_FILES['uploadFile']['tmp_name'], 'rb');
while ( ($line = fgets($fp)) !== false) {
echo "$line<br>";
}
you do not need to save it.
你不需要保存它。
回答by John Conde
You would need to move the file to where you want it to go firstthen you can work with it. See move_uploaded_file()for how this is done. See also, Handling File Uploadsin the PHP docs particularly the page showing how to do with using POSTwhich you are doing.
您需要先将文件移动到您想要的位置,然后才能使用它。看看move_uploaded_file()这是如何完成的。另请参阅PHP 文档中的处理文件上传,特别是显示如何使用您正在执行的POST 的页面。
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
回答by flowfree
Use $_FILES["file"]["tmp_name"]instead. And get the contents of the file with
使用$_FILES["file"]["tmp_name"]来代替。并获取文件的内容
$contents = file_get_contents($_FILES['file']['tmp_name']);
回答by LZNPDE
Could it be that there is an open_basedir restriction?
会不会有 open_basedir 限制?
open_basedir limits the files that can be opened by PHP within a directory-tree.
open_basedir 限制了 PHP 在目录树中可以打开的文件。
回答by siwalikm
You can read the whole text file into an array with php explode fn.
您可以使用 phpexplode fn 将整个文本文件读入一个数组。
eg. $wholeTextFile = explode("\n", file_get_contents('textFileName.txt'));
Then get the total number of lines by counting sizeOfarray.
然后通过计数sizeOf数组得到总行数。
eg. $totalLinesinFile = sizeOf($wholeTextFile);
Now when you iterate the array in a forloop or forEachloop, each iteration gives you 1 line.
现在,当您在for循环或forEach循环中迭代数组时,每次迭代都会给您 1 行。
eg.
for ($i = 0; $i < $totalLinesinFile ; $i++) {
echo "Line number is ".$i."<br>";
echo wholeTextFile[$i]."<hr>";
}

