Getting a File Name

Discussion in 'PHP' started by Kennedy, Mar 4, 2008.

  1. #1
    Is there any way to get the names of files in a folder? For example, I have 3 files in this folder:

    index.html
    script.php
    thisname.php
    thisothername.php

    I want the script to grab the names of the files in the folder with it. Is there any way to do this?
     
    Kennedy, Mar 4, 2008 IP
  2. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There is readdir($handle);

    Take a look at the explanation on php.net/readdir to see the best way to implement it in your site.
     
    Altari, Mar 5, 2008 IP
  3. 00johnny

    00johnny Peon

    Messages:
    149
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    glob is another one:

    $textFiles = glob("*.txt")
    PHP:
    would fill $textFiles with an array of files ending in *.txt
    its a pretty useful function...
     
    00johnny, Mar 5, 2008 IP
  4. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    scandir() is yet another alternative, for PHP 5.

    It opens a directory, creates a list of all files, and closes the directory.

    There's one minor difference between it and using readdir() (that I've noticed so far).

    Readdir() actually accesses each file while it reads through the directory, so it resets the last accessed time for the file. scandir() doesn't access the file, so the last access time remains unchanged.

    - Walkere
     
    walkere, Mar 5, 2008 IP
  5. 9450184

    9450184 Peon

    Messages:
    30
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $dir = "folder";
    $handle = opendir($dir);
    while($file = readdir($handle))
    	{
    	if($file != "." && $file != "..")
    		{
    		print "<p>$file</p>";
    		}
    	}
    closedir($handle);
    PHP:
     
    9450184, Mar 5, 2008 IP