English 中文(简体)
如何在 PHP 中上传. txt 文件, 并在另一页上逐行读取?
原标题:How does one upload a .txt file in PHP and have it read line by line on another page?

我的目标是在窗体(浏览)上上传.txt 文件,将文件张贴到另一个 php 页面,然后将该文件逐行读取。

My code so far is here. FILE 1: HTML UPLOAD:

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

视频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");
}

正如我所看到的,第二个文件将证实没有错误, 上传的文件是. txt。 然后它会打开( ) 文件, 然后我就能用 fgets () (() (我设法让所有文件都起作用) 阅读 。

然而,只有当正在上传的.txt 文件恰好与 PHP 文件在同一目录中时,该代码才有效。 否则我就会收到许多错误信息。 当您无法上传不在 PHP 文件文件夹中的文件时, 它会破坏最初拥有文件上传系统的目的 。

谁能告诉我这个密码有什么问题吗?

问题回答

逐行读一行, 它为我工作 这个代码:

$fp = fopen($_FILES[ uploadFile ][ tmp_name ],  rb );
    while ( ($line = fgets($fp)) !== false) {
      echo "$line<br>";
    }

您不需要保存它。

使用 $$_FILES [“file”][“tmp_name”] 代替。然后获取文件内容

$contents = file_get_contents($_FILES[ file ][ tmp_name ]);

您需要将文件移动到您想要它去的地方 first , 然后您就可以使用它。 见 < a href="https://www.php. net/ manual/ en/ featals. file.php" rel= "nofollown noreferr". move-uploaded- file.php" rel= "nofollowed- pile. mivep" rel= "nofollowing file 特别在PHPD docs 尤其是 < a href=> "https://www.php. net/ manual/ en/ featorantes. file-upload. p" rel= "nofolrererer" 页面, 显示如何使用 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.
";
} else {
    echo "Possible file upload attack!
";
}

echo  Here is some more debugging info: ;
print_r($_FILES);

print "</pre>";

?>

您可以用 php 爆炸 fn 将整个文本文件读入数组。

eg. $wholeTextFile = explode("
", file_get_contents( textFileName.txt ));

然后通过计算 < code> size of 数组获得行的总数。

eg. $totalLinesinFile = sizeOf($wholeTextFile);

当您在 roil 或 forEach roil 或 forEach roil 的 中将数组循环时,每次迭代给您一行。

eg.

for ($i = 0; $i < $totalLinesinFile ; $i++) {
   echo "Line number is ".$i."<br>";
   echo wholeTextFile[$i]."<hr>";
}

会不会有开放限制?

Open_basedir 限制 PHP 在目录树内可以打开的文件 。

http://www.php.net/manual/en/ini.core.php#ini.open-basiir" rel="no follow">http://www.php.net/manual/en/ini.core.phpini.open-basir





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...