mojito Posted January 18, 2007 Share Posted January 18, 2007 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 https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/ Share on other sites More sharing options...
taith Posted January 18, 2007 Share Posted January 18, 2007 simplify...[code]$xmlfiles=glob('yourfolder/*.xml');[/code]and the "if ($file != "." && $file != "..")" removes the two directories from the top... (.==self ..==parent) Link to comment https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163572 Share on other sites More sharing options...
tuuga Posted January 18, 2007 Share Posted January 18, 2007 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 https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163574 Share on other sites More sharing options...
mojito Posted January 18, 2007 Author Share Posted January 18, 2007 That last bit of code is working well except for scope, I cant read $allowTypes=array("xml","txt"); which I have set outside the function.I think I need to modify some php.ini values to read it, or declare it global ? Link to comment https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163590 Share on other sites More sharing options...
mojito Posted January 18, 2007 Author Share Posted January 18, 2007 I get the [font=Verdana]unexpected T_GLOBAL[/font] when using inside function [ global $allowTypes]something not set up right in my config ? Link to comment https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163594 Share on other sites More sharing options...
trq Posted January 18, 2007 Share Posted January 18, 2007 Whats wrong with?[code=php:0]$xmlfiles=glob('yourfolder/*.xml');[/code] Link to comment https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163597 Share on other sites More sharing options...
mojito Posted January 18, 2007 Author Share Posted January 18, 2007 I didnt understand it or how to use it, i can appreciate its simplicity and wildcard but what is glob ?I do thank you for your neat coding, i'm a flash guy normally so struggle with all this php.thanks Link to comment https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163603 Share on other sites More sharing options...
trq Posted January 18, 2007 Share Posted January 18, 2007 If you dont know how a function works, [url=http://php.net/glob]look it up[/url]. Link to comment https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163609 Share on other sites More sharing options...
taith Posted January 18, 2007 Share Posted January 18, 2007 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]outputarray("0"=>"test.xml", "1"=>"blah.xml")[/code] Link to comment https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163610 Share on other sites More sharing options...
mojito Posted January 18, 2007 Author Share Posted January 18, 2007 ahhh, that seems simpler glob looked like a custom function. Thanks I will use that. Link to comment https://forums.phpfreaks.com/topic/34709-checking-for-specific-files-via-extensions-in-a-folder/#findComment-163621 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.