Jump to content

Checking for specific files via extensions in a folder


mojito

Recommended Posts

I want to list only xml files in a folder, so far I have the directory showing all files..[code]

$file_dir="test";

$dir=opendir($file_dir);
while ($file=readdir($dir))
{
if ($file != "." && $file != "..")
{
echo "<a href=".$file_dir."/".$file." target=_blank>".$file."</a>";
echo "<br>";
}
}

[/code]

what is the following line doing ? [font=Verdana]if ($file != "." && $file != "..")[/font]

How can I read the extension, do I have to manually do the string operation of looking for the last "."

thanks
i use this function

[CODE]
//$catName -- string ex: './pictures/'
//$allowTypes (optional) - set file types -- array ex: $allowTypes = array('jpg', 'gif');
function openPics ($catName, $allowTypes=NULL) {
//open catalog
$open = opendir($catName);
//check every file
while (false !== ($file = readdir($open))) {
if ($file != '.' && $file != '..' && !is_dir($file)) {
$ex = explode('.', $file);
//if allowTypes is set
if (isset($allowTypes)) {
//if is right type
$type = $ex[count($ex)-1];
if (in_array($type, $allowTypes)) {
$pictures[] = $file;
}
}
//take all file names in array
else {
$pictures[] = $file;
}
}
}
return $pictures;
closedir($open);
}
[/CODE]
glob() looks in the specified folder, and returns anything alikened to it... *<-- all files *.xml <-- all xml files etc... therefore...

[code]$xmlfiles=glob('yourfolder/*.xml');
print_r($xmlfiles);[/code]

that makes $xmlfiles an array() of all the files in that folder.

[code]
output
array("0"=>"test.xml", "1"=>"blah.xml")
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.