cooldude832 Posted June 18, 2007 Share Posted June 18, 2007 I'm trying to find all the include files in a folder is there a way to some how read a folder and then return an array with all the file names as the array keys or associative keys? I only want to return files that are .html or .php not all files Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/ Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 Would this work? $dir = "/subfolder"; $files = scandir($dir); $i = 0; foreach($files as $value) { $temp = explode(".",$value); if ($temp[1] == ".html" || $temp[1] == ".php" || $temp[1] == ".htm") { $file[$i] = $value; $i++; } } Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277102 Share on other sites More sharing options...
Dragen Posted June 18, 2007 Share Posted June 18, 2007 It should work. The only problem is if there is more than one '.' in the filename.. such as in a file called myfile.ini.php, which I've seen before. You could use this, which will use the last value from the explode (whgich will be the filetype). <?php $dir = "/subfolder"; $files = scandir($dir); $i = 0; foreach($files as $value) { $temp = explode(".",$value); if ((end($temp) == "html") || (end($temp) == "php") || (end($temp) == "htm")) { $file[$i] = $value; $i++ } } ?> or this should work.. and be a bit neater. First set an array with the filtypes in, then search the array. If the filetype isn't in the array it's ignored. <?php $filetype = array('html', 'htm', 'php'); $dir = "/subfolder"; $files = scandir($dir); $i = 0; foreach($files as $value) { $temp = explode(".",$value); if(array_search(end($temp), $filetype) != false) { $file[$i] = $value; $i++ } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277114 Share on other sites More sharing options...
GingerRobot Posted June 18, 2007 Share Posted June 18, 2007 Looks alright to me. Have you tried it? Dont forget that scandir() is a php 5 function. If you are running a version prior to that, you'll need to use opendir() and readdir(). Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277115 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 i'm on 4.4 can you show me how to do it in 4 i always forget to check that on php.net that idea sounds good for the reading extensions, event though i will be inputting the files it will still be a good idea Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277117 Share on other sites More sharing options...
GingerRobot Posted June 18, 2007 Share Posted June 18, 2007 Probably just as easy for you to read it off of the php.net website: http://uk.php.net/readdir Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277120 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 How does this sound for a php-4 version? <?php $dir = "/subfolder"; if(is_dir($dir)) { $files = opendir($dir); $files = readdir($files); $i = 0; foreach($files as $value) { $temp = explode(".",$value); if ((end($temp) == "html") || (end($temp) == "php") || (end($temp) == "htm")) { $file[$i] = $value; $i++ } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277122 Share on other sites More sharing options...
Dragen Posted June 18, 2007 Share Posted June 18, 2007 looks fine to me I'd still suggest storing the filetypes to check in an array, but it's up to you. What you've got should work fine Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277126 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 I agree with you so i can add more file types easily and mod it for other types How does this sound for a php-4 version? <?php $filetype = array('html', 'htm', 'php'); $dir = "/subfolder"; if(is_dir($dir)) { $files = opendir($dir); $files = readdir($files); $i = 0; foreach($files as $value) { $temp = explode(".",$value); if(array_search(end($temp), $filetype) != false) { $file[$i] = $value; $i++ } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277129 Share on other sites More sharing options...
Orio Posted June 18, 2007 Share Posted June 18, 2007 I much easier way, and a more originaized one imho: <?php $filetypes = array("htm", "html", "php"); $files = array(); foreach($filetypes as $type) $files[$type] = glob("*.".$type); ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277136 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 gettign errors and my chmod is set to allow all to read it Warning: opendir() [function.opendir]: open_basedir restriction in effect. File(/sources) is not within the allowed path(s): (/hsphere/local/home/pira00:/usr/local/lib/php:/hsphere/shared/apache/libexec:/tmp:/var/tmp) in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 4 Warning: opendir(/sources) [function.opendir]: failed to open dir: Operation not permitted in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 4 Warning: readdir(): supplied argument is not a valid Directory resource in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 5 Warning: Invalid argument supplied for foreach() in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277140 Share on other sites More sharing options...
Dragen Posted June 18, 2007 Share Posted June 18, 2007 try changing this: $dir = "/subfolder"; to this: $dir = "../subfolder"; Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277144 Share on other sites More sharing options...
Orio Posted June 18, 2007 Share Posted June 18, 2007 Try using the script I provided above... Orio. Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277145 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 Warning: opendir(../sources) [function.opendir]: failed to open dir: No such file or directory in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 4 Warning: readdir(): supplied argument is not a valid Directory resource in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 5 Warning: Invalid argument supplied for foreach() in /hsphere/local/home/pira00/upperstraitscleanlake.org/About/index_src.php on line 8 now its saying not a dir the file its part of is an included file in the loaded doc, but the include is in the same folder as the actual page http://upperstraitscleanlake.org/About/index.php Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277148 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 no offense orio but your script is completly down river of the error Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277150 Share on other sites More sharing options...
Orio Posted June 18, 2007 Share Posted June 18, 2007 no offense orio but your script is completly down river of the error What do you mean? Orio. Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277152 Share on other sites More sharing options...
Dragen Posted June 18, 2007 Share Posted June 18, 2007 if it's in the same directory then use this: $dir = "subfolder"; no leading slash Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277156 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 tried that and orio your talking about somethign that the code isn't getting to the file extension array isn't it i can't open the directory Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277161 Share on other sites More sharing options...
Dragen Posted June 18, 2007 Share Posted June 18, 2007 where is the file in relation to index_src.php? Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277169 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 okay from my root Root -> About -> sources index.php dir i'm trying to open index_src.php Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277171 Share on other sites More sharing options...
Dragen Posted June 18, 2007 Share Posted June 18, 2007 $dir = dirname($_SERVER["PHP_SELF"]); Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277174 Share on other sites More sharing options...
cooldude832 Posted June 18, 2007 Author Share Posted June 18, 2007 $thisdir = dirname($_SERVER["PHP_SELF"]); echo "dir: ".$thisdir."<br/>"; returns: dir: /About Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277178 Share on other sites More sharing options...
Dragen Posted June 19, 2007 Share Posted June 19, 2007 just found this, thanks to ACE: getcwd try this: $dir = getcwd(); Quote Link to comment https://forums.phpfreaks.com/topic/56105-is-there-a-way-to-return-all-the-html-files-in-a-folder/#findComment-277509 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.