1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Getting Filenames from a directory into an array

Discussion in 'PHP' started by RunningRunners, Oct 27, 2012.

  1. #1
    Hello, I have a folder called csv which resides on public_html. All the files are .csv files. How do I get all the file names into an array, without having the extension.

    Thanks
     
    RunningRunners, Oct 27, 2012 IP
  2. knewedge

    knewedge Active Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #2
    knewedge, Oct 27, 2012 IP
  3. thanabuts

    thanabuts Peon

    Messages:
    13
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $folderArr = ("myfolder/*.csv");
    foreach ($folderArr as $filename){
    echo $filename."\n";
    }
     
    thanabuts, Oct 29, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    *SIGH*, people are dumber for having read the responses so far. As a rule of thumb you can't trust explode since you don't know how many periods are in a filename since there's no real restriction on that anymore... and I have no clue what "thatabuts" is trying to do as that's just gibberish.

    The correct answer is PHP's pathinfo command.
    http://www.php.net/manual/en/function.pathinfo.php

    Preferably after a glob to actually pull the directory using a filter.
    http://php.net/manual/en/function.glob.php

    
    $fileList=glob('csv/*.csv');
    foreach ($fileList as $fileName) {
      echo pathinfo($fileName,PATHINFO_FILENAME),'<br />';
    }
    
    Code (markup):
    Note that pathinfo can be used to pull an array containing all the values, or you can pass different named constants to it to pull information like PATHINFO_EXTENSION, PATHINFO_DIRECTORY or even PATHINFO_BASENAME.
     
    deathshadow, Oct 30, 2012 IP
  5. knewedge

    knewedge Active Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #5
    explode the whole extension then.
     
    knewedge, Oct 31, 2012 IP