0

hi. i want to load and read a file in php. i wrotte this code but it doesn't work.
i want when the user loads a txt file, then it will be shown in the page. i can use fopen but i dont know its directory for example. any help ?

<input type="file" name="doc" />

$file = fopen($_FILES['doc'], "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  echo fgets($file). "<br>";
  }
fclose($file);
?> 
2
Contributors
3
Replies
21
Views
4 Years
Discussion Span
Last Post by NardCake
1

If you're just trying to read the whole file it would be much easier to use the file_get_contents function so like this:

$file = file_get_contents('test.txt');

You can use a file path or url (depending on your server permissions).

0

i want to read a file that i can load. not a fixed url. if you know what i mean.i want to load a file and read it.

1

Also regarding your html, it's not in a form tag!

form method="post" enctype="multipart/form-data">
<input type="file" name="file">
</form>

Then in php to the value of the uploaded document would be the temporary location:

$_FILES['file']['tmp_name']

Sirry didn't thoroughly read for first response.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.