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

Micheal Russell
PLUS
Micheal Russell
Courses Plus Student 1,312 Points

Bummer! I do not see the h3 tags. I cannot get passed this error. What am I missing in this course

The h3 tags are there the code works why am I getting this error. What lesson was it in.

index.php
<?php
//add code here
$files = scandir('example');
$dir = 'example' ;
if ( $dh = opendir ( $dir )) {
while (( $file = readdir ( $dh )) !== false ) {
if ( substr ( $file , 0, 3) !== 'fun' ) {
//do something with $file
  echo $file;
} else { 
  echo "<h3>filename: " . $file . "</h3>";
}
}
closedir ( $dh );
}
?>

3 Answers

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Micheal

  1. You have to check for the value of fun within the file name, not just at the start. Use the strpos() function for this.
  2. You don't need the else on your condition. If the string fun can be found within any file, perform the echo() statement, there is no else.
  3. Only print out what is required. Adding extra text, such as filename: between the tags, can throw the engine off and give you an incorrect answer.

Just as a note, in the first step you obtained an array of all the files within the example directory, so you don't the while loop to read the contents of the directory again. It's much simpler to use the $files array in a foreach loop.

$files = scandir('example');
foreach ($files as $file) {
    // do something with $file
}
Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Micheal Russell Umesh Ravji

I am confused as to why this response was originally 'downvoted'?? (Which I have removed and have marked as Best Answer).

The answer is clear and provides all the necessary information to work through the errors in the code from the original post. In fact, in my opinion, this is a very well thought out and explained answer. Thank-you Umesh.

Jason ~~ Treehouse Community Moderator

Micheal Russell
PLUS
Micheal Russell
Courses Plus Student 1,312 Points

umesh you are the greatest I am down for the night but in the am i will apply your reply thank you.

Umesh Ravji
Umesh Ravji
42,386 Points

Did it not work? The downvote?

Micheal Russell
PLUS
Micheal Russell
Courses Plus Student 1,312 Points

I believe you asked what lesson it was. it is File Handling with PHP , step 4 Reading Files