HTML/PHP Get the content of a .txt file user-side with file_get_contents()

374

I am trying to print on the page the content of a.txt file that the user would have uploaded beforehand.

This is the code I have so far:

<form action="page.php" method="POST">
    <input type="file" name="userfile"/>
    <input type="submit" value="Load"/>
</form>

On page.php :

<?php
    echo file_get_contents($_POST['userfile']);
?>

The error I get:

"Warning: file_get_contents(userfile.txt): failed to open stream: No such file or directory"

It does work when the.txt file is in the same directory as thepage.php.

But the files that will be printed won't be in this directory, on the server.

How can I do that?

Thanks for your answers!

289

Answer

Solution:

Use enctype="multipart/form-data" in form

<form action="index.php" method="POST" enctype="multipart/form-data">

In PHP

echo file_get_contents($_FILES['userfile']['tmp_name']);

People are also looking for solutions to the problem: html - PHP unknown column number

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.