How does one upload a .txt file in PHP and have it

2019-04-06 14:34发布

问题:

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.

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>

FILE 2: READING THE FILE

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

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.

Can someone tell me what is wrong with this code?

回答1:

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.



回答2:

You would need to move the file to where you want it to go first then you can work with it. See move_uploaded_file() for how this is done. See also, Handling File Uploads in the PHP docs particularly the page showing how to do with using POST which you are doing.

<?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>";

?>


回答3:

Use $_FILES["file"]["tmp_name"] instead. And get the contents of the file with

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


回答4:

Could it be that there is an open_basedir restriction?

open_basedir limits the files that can be opened by PHP within a directory-tree.

http://www.php.net/manual/en/ini.core.php#ini.open-basedir



回答5:

You can read the whole text file into an array with php explode fn.

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

Then get the total number of lines by counting sizeOf array.

eg. $totalLinesinFile = sizeOf($wholeTextFile);

Now when you iterate the array in a for loop or forEach loop, each iteration gives you 1 line.

eg.

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