Jump to content

How to exclude file extensions?


JsusSalv

Recommended Posts

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

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

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

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