PHP Get file names from folder and store in array

Posted on 2011-04-24
Last Modified: 2012-08-13
Hey guys and gals,
This is my first step into PHP!  I have built quite a few basic programs using ASP, but this is my first with PHP.  

I am trying to get a list of file names from a folder and then store them in an array.  Any ideas on how I can do this?

Please remember I am new!

Question by:Slim81
    LVL 18

    Accepted Solution

    Try this:
    if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $file_array[] = $file;
    //print the files

    Open in new window

    LVL 30

    Expert Comment

    by:Marco Gasi
    Or try

    $dir = 'your_dir_path';
    $filelist = scandir($dir) or die('Invalid directory name');
    foreach ($filelist as $file){
      if (is_file('$dir/$file') && $file != '.' && $file != '..'){
        $file_array[] = $file;

    Open in new window

    You can also use an iterator:
    $dir = 'your_dir_path';
    $iterator = new DirectoryIterator($dir);
    //back to the begin of directory
    while ($iterator->valid()){
      if ($iterator->isFile() && !$iterator->isDot()){
        $file_array[] = $iterator->getFilename();

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now