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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.