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

Array issues

Whenever I execute my code on the task, it is saying that the array is empty, may you please advice on why and how to proceed with the task

index.php
<?php
//add code here
$files = file('example');
Victoria Loisi
Victoria Loisi
7,070 Points

Hello there, Have you tried using 'var_dump($files)' to check what it has inside?

1 Answer

Jonathan Love
Jonathan Love
4,784 Points

Your array variable is empty because you declared it but you didn't do anything with it.

You have to use one of the PHP directory functions to look for the files. Go to the first video and and take a look at the teacher's notes and also take a look at the "File Handling Cheat Sheet".

  • chdir — Change directory
  • chroot — Change the root directory
  • closedir — Close directory handle
  • dir — Return an instance of the Directory class
  • getcwd — Gets the current working directory
  • opendir — Open directory handle
  • readdir — Read entry from directory handle
  • rewinddir — Rewind directory handle
  • * scandir — List files and directories inside the specified path *