Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP File Handling with PHP Basic File Handling Reading Files

Adi Chayot
Adi Chayot
3,618 Points

Can someone point where i am getting wrong?

-

index.php
<?php
//add code here
$files = array();

$myfile = fopen("example", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
  $line = fgets($$myfile);
  array_push($files, $line);
}

echo $files;

1 Answer

Steven Parker
Steven Parker
229,607 Points

You're working too hard! There's a function that will give you the contents of a directory in an array all in one step.

The function's name is "scandir".

Adi Chayot
Adi Chayot
3,618 Points

Thank you for your answer I want to try by the long way, as i asked in this coding quastion.

Any idea why this is not working?

Steven Parker
Steven Parker
229,607 Points

If you really want "the long way", you still need to use other directory functions (like opendir and readdir) instead of file functions (like fopen and fgets). And testing for the end will also be done differently. There's a Secondary Example in the Teacher's Notes section of the first video in this stage.

One other issue is that you have a "$$myfile" (with an extra "$").