JsusSalv Posted May 21, 2008 Share Posted May 21, 2008 Hello: I have a script that displays only desired directories and files. Everything listed within the if statement is excluded. There are two things I want help with: 1) Using similar principles as within my code, how can I exclude certain extensions??? 2) Is there a way exclude directories (and subdirectories) without having to list each directory manually as I am have done below? I'd like the script to just be able to detect directories and automagically keep them from being listed. This should be simple and straightforward but for some reason the solutions elude me. Can someone help figure this out? Any help would be appreciated! Thank you everyone! Here's the code I have so far: <?php $dir = '/path/to/root/directory/'; // Path to root directory. $files = scandir($dir, 0); for( $ctr = 1; $ctr < sizeof( $files ); $ctr++ ) { // Do not list any files, extensions, directories, or subdirectories that are not supposed to be listed. // Make sure the Logical AND (&&) is listed at the end of each file name. if( // Don't list the '.' $files[$ctr] != "." && // Don't list the '..' $files[$ctr] != ".." && // List any file types or extensions that shouldn't be displayed. ????????????????????? ????????????????????? // Don't list any of the following directories. $files[$ctr] != "CMS" && $files[$ctr] != "CSS" && $files[$ctr] != "Flash" && $files[$ctr] != "Images" && $files[$ctr] != "Includes" && $files[$ctr] != "JS" && $files[$ctr] != "Login_System" && $files[$ctr] != "Payment_Gateway" && $files[$ctr] != "PDFs" && $files[$ctr] != "PHP" && // Don't list any of the following files. $files[$ctr] != ".htaccess" && $files[$ctr] != "most_recent.php" && $files[$ctr] != "pdf_upload.php" ) print "<span>$files[$ctr]</span><br /><br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/106593-how-to-exclude-file-extensions/ Share on other sites More sharing options...
GingerRobot Posted May 21, 2008 Share Posted May 21, 2008 Eugh. Use arrays! <?php $dir = '/path/to/root/directory/'; // Path to root directory. $files = scandir($dir, 0); $excluded_files = array('CMS','CSS','FLASH','.htaccess')//etc $excluded_extensions = array('php','inc.php');//etc for( $ctr = 1; $ctr < sizeof( $files ); $ctr++ ) { list($file,$ext) = explode('.',$fils[$ctr],2); if(!in_array($files[$ctr],$excluded_files) && !in_array($ext,$excluded_extensions)){ print "<span>$files[$ctr]</span>\\n"; } } ?> If you didn't want to include any of the directories, you could use the is_dir() function and save yourself some typing. Link to comment https://forums.phpfreaks.com/topic/106593-how-to-exclude-file-extensions/#findComment-546341 Share on other sites More sharing options...
JsusSalv Posted May 21, 2008 Author Share Posted May 21, 2008 Great idea! A few more questions: 1) I modified the code a bit and to get what I wanted: <?php // Path to root directory. $dir = '/path/to/root/directory/'; $files = scandir($dir, 0); // Exclude the following directories. $excluded_directories = array('CMS','CSS','Flash', 'Images', 'Includes', 'JS', 'PDFs'); // Exclude the following files. $excluded_files = array('.htaccess', 'directory_listing2.php'); //etc // Exclude the following extensions. $excluded_extensions = array('kpf'); //etc for( $ctr = 1; $ctr < sizeof( $files ); $ctr++ ) { list($file,$ext) = explode('.',$files[$ctr],2); if(!in_array($files[$ctr],$excluded_directories) && !in_array($files[$ctr],$excluded_files) && !in_array($ext,$excluded_extensions) && $files[$ctr] != "." && $files[$ctr] != ".."){ print "<span>$files[$ctr]</span><br />"; } } ?> 2) So is there a way to detect directories and somehow exclude them from displaying? Or, as in the above example, do I just have to list them out individually?? 3) list($file,$ext) = explode('.',$files[$ctr],2); What does $file within this line resolve to? I thought it was missing the 's' but when I added it everything went blank. Can you elaborate on this a bit? Thank you! Link to comment https://forums.phpfreaks.com/topic/106593-how-to-exclude-file-extensions/#findComment-546354 Share on other sites More sharing options...
GingerRobot Posted May 21, 2008 Share Posted May 21, 2008 2.) Yes - to exclude all directories, add !is_dir($files[$ctr]) to your if statement. 3.) The explode function separates the string by a full stop, and it is broken into two pieces (forced with the second parameter to allow for extensions such as .inc.php) The list function allows you to create variables from an array. Therfore, $file is the first part of the string --that is, the bit before the first full stop -- and is infact completely useless to us. We're only interested in the part after the full stop, which we call $ext. Make sense? Oh, and when you post your code in future, dont forget to use the tags please Link to comment https://forums.phpfreaks.com/topic/106593-how-to-exclude-file-extensions/#findComment-546570 Share on other sites More sharing options...
JsusSalv Posted May 21, 2008 Author Share Posted May 21, 2008 Thank you!. I will use code tags...sorry about that. Yes, it all makes sense and great explanation about everything. Link to comment https://forums.phpfreaks.com/topic/106593-how-to-exclude-file-extensions/#findComment-546683 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.