Most Referenced Functions
  » google_pagerank()
  » preg_replace()
  » imagecreatefrompng()
  » site_pageranks()
  » imagepng()
  » imagedestroy()
  » imagestring()
  » imagecolorallocate()
  » htmlentities()
  » preg_match()
  » fopen()
  » header()
  » getimagesize()
  » preg_match_all()
  » session_start()
  » htmlspecialchars()
  » ob_start()
  » ob_flush()
  » strstr()
  » flush()
  » strpos()
  » setcookie()
  » str_replace()
  » array2vars()
  » nl2br()
  » preg_split()
  » ereg()
  » ereg_replace()
  » urlencode()
  » readgzfile()

Become a sponsor for $15/month. Link is sitewide - PR5 homepage, 20+ PR4 pages, 90+ PR3 pages. Email dave[AT]icemelon[D0T]c0m.

PHP Functions

Function: scandir

(PHP 5)

scandir -- List files and directories inside the specified path

Description

array scandir ( string directory [, int sorting_order [, resource context]] )

Returns an array of files and directories from the directory .

Parameters

directory

The directory that will be scanned.

sorting_order

By default, the sorted order is alphabetical in ascending order. If the optional sorting_order is used (set to 1), then the sort order is alphabetical in descending order.

context

For a description of the context parameter, refer to the streams section of the manual.

Return Values

Returns an array of filenames on success, or FALSE on failure. If directory is not a directory, then boolean FALSE is returned, and an error of level E_WARNING is generated.

Examples

Example 1. A simple scandir() example

<?php
$dir    = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);

print_r($files1);
print_r($files2);
?>

The above example will output something similar to:

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)

Example 2. PHP 4 alternatives to scandir()

<?php
$dir = "/tmp";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

print_r($files);

?>

The above example will output something similar to:

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)

Notes

Tip: You can use a URL as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename and Appendix L for a list of supported URL protocols.

Related Function(s)

  • fopen()
  • opendir()
  • readdir()
  • glob()
  • is_dir()
  • sort()
  • dom_domattr_construct()
  • dom_domattr_isid()
  • dom_domcharacterdata_appenddata()
  • dom_domcharacterdata_deletedata()
  • dom_domcharacterdata_insertdata()
  • dom_domcharacterdata_replacedata()
  • dom_domcharacterdata_substringdata()
  • dom_domcomment_construct()
  • dom_domdocument_construct()
  • dom_domdocument_createattribute()
  • dom_domdocument_createattributens()
  • dom_domdocument_createcdatasection()
  • dom_domdocument_createcomment()
  • dom_domdocument_createdocumentfragment()
  • dom_domdocument_createelement()
  • dom_domdocument_createelementns()
  • dom_domdocument_createentityreference()
  • dom_domdocument_createprocessinginstruction()
  • dom_domdocument_createtextnode()
  • dom_domdocument_getelementbyid()
  • dom_domdocument_getelementsbytagname()
  • dom_domdocument_getelementsbytagnamens()
  • dom_domdocument_importnode()
  • dom_domdocument_load()
  • dom_domdocument_loadhtml()
  • dom_domdocument_loadhtmlfile()
  • dom_domdocument_loadxml()
  • dom_domdocument_normalize()
  • dom_domdocument_relaxngvalidate()
  • dom_domdocument_relaxngvalidatesource()
  • dom_domdocument_save()
  • dom_domdocument_savehtml()
  • dom_domdocument_savehtmlfile()
  • dom_domdocument_savexml()
  • dom_domdocument_schemavalidate()
  • dom_domdocument_schemavalidatesource()
  • dom_domdocument_validate()
  • dom_domdocument_xinclude()
  • dom_domelement_construct()
  • dom_domelement_getattribute()
  • dom_domelement_getattributenode()
  • dom_domelement_getattributenodens()
  • dom_domelement_getattributens()
  • dom_domelement_getelementsbytagname()
  • dom_domelement_getelementsbytagnamens()
  • dom_domelement_hasattribute()
  • dom_domelement_hasattributens()
  • dom_domelement_removeattribute()
  • dom_domelement_removeattributenode()
  • dom_domelement_removeattributens()
  • dom_domelement_setattribute()
  • dom_domelement_setattributenode()
  • dom_domelement_setattributenodens()
  • dom_domelement_setattributens()
  • dom_domentityreference_construct()
  • dom_domimplementation_construct()
  • dom_domimplementation_createdocument()
  • dom_domimplementation_createdocumenttype()
  • dom_domimplementation_hasfeature()
  • dom_domnode_appendchild()
  • dom_domnode_clonenode()
  • dom_domnode_hasattributes()
  • dom_domnode_haschildnodes()
  • dom_domnode_insertbefore()
  • dom_domnode_issamenode()
  • dom_domnode_issupported()
  • dom_domnode_lookupnamespaceuri()
  • dom_domnode_lookupprefix()
  • dom_domnode_normalize()
  • dom_domnode_removechild()
  • dom_domnode_replacechild()
  • dom_domnodelist_item()
  • dom_domprocessinginstruction_construct()
  • dom_domtext_construct()
  • dom_domtext_iswhitespaceinelementcontent()
  • dom_domtext_splittext()
  • dom_domxpath_construct()
  • dom_domxpath_registernamespace()
  • dom_domxpath_evaluate()
  • dom_domxpath_query()
  • dom_domnamednodemap_getnameditem()
  • dom_domnamednodemap_getnameditemns()
  • dom_domnamednodemap_item()
  • dom_import_simplexml()
  • Icemelon -- PHP, CSS, Javascript Tutorials, & More!
      © 2005-2010 Icemelon.com   Email: dave[AT]icemelon[D0T]c0m