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

Recommended Answers

All 3 Replies

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.