Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,070 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
221,070 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 "$").