note 29329 added to function.opendir
| From: | Artistan at hotmail dot com | Date: | Mon, 10 Feb 2003 04:55:09 +0000 |
| Subject: | note 29329 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43720@lists.php.net to get a copy of this message | ||
Read files in folder and all sub folders.
Newbie,
message me with your thoughts on this...............................
tried code from all over until i got it to do what i wanted
function getfilesall($foldername)
Stores files in an array including files in all subfolders given the folder name
function getfilesone($foldername)
Stores files ina an array from given folder name.
checks if folder or not with
if(!strchr($fil[$x],".")){.........}
//files.php
<?php
//-----------------------------------------------
// USER CONFIGURABLE ELEMENTS
//-----------------------------------------------
// Root path
$root_path = "./";
//-----------------------------------------------
require $root_path."filesfunction.php";
//------------------------------------------------
//getfilesall('images'); //Test
//given a folder name, gets all filenames within the folder and subfolders
//------------------------------------------------
function getfilesall($foldername)
{
$folder = "$foldername\\";
// fill an array with all items from a directory
$arrayfiles = readfolderfiles($folder);
//gets all subfolder info
$filesarray = nextfolder($arrayfiles, $folder);
//echo "Check This Out.";
$result = count((array)$filesarray);
//echo " $result <BR>";
//print_r($filesarray);
//echo "<BR>";
return $filesarray;
}
//------------------------------------------------
//getfilesone('images'); //Test
//given a folder name, gets all filenames within that folder
//------------------------------------------------
function getfilesone($foldername)
{
$folder = "$foldername\\";
// fill an array with all items from a directory
$arrayfiles = readfolderfiles($folder);
//echo "Check This Out.";
//$result = count((array)$arrayfiles);
//echo " $result <BR>";
//print_r($arrayfiles);
//echo "<BR>";
return $arrayfiles;
}
?>
//filefunctions.php
<?php
//*********************************************
//User Function
//function to read folder files and return them in an array
function readfolderfiles($folder)
{
// fill an array with all items from a directory
$handle = opendir($folder);
while ($file = readdir($handle))
{
$files[] = $folder.$file;
}
closedir($handle);
$result = count($files);
//echo "$result <BR>";
sort($files);
//print_r($files);
//echo "<BR>";
return $files;
}
function nextfolder($fil,$folder2)
{ //*
$storefiles = (array)$fil;
$result = count($fil);
$x = 0;
while($x <= $result-1)
{
//echo "$fil[$x] <BR>";
if(!strchr($fil[$x],"."))
{
$nextfolder = "{$fil[$x]}\\";
//echo($nextfolder);
$Xfiles = readfolderfiles($nextfolder);
$storefiles = array_merge($storefiles, $Xfiles);
}
$x = $x + 1;
}
$fil = array_merge($fil, $storefiles);
return $fil;
//*/
}
?>
--
http://www.php.net/manual/en/function.opendir.php
http://master.php.net/manage/user-notes.php?action=edit+29329
http://master.php.net/manage/user-notes.php?action=delete+29329
http://master.php.net/manage/user-notes.php?action=reject+29329