Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    exclude files using arrays

    Hi I am trying to exclude more then one file and more then one extension
    can someone show the proper way to implement arrays for $excludeExt
    and $excludeFile vars below.

    PHP Code:
    CreateRefFile($path,$level+1,$last+1,$files$main_dir); // uses recursion
            
    global $excludeFile$excludeExt;
                    } else {
                    if ((
    strcmp($file$excludeFile) == 0) || (substr($file, -4) === $excludeExt))
                          continue; 
    Thanks
    Sonny
    Last edited by sonny; 10-25-2012 at 04:47 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    global is not a good idea.
    What is that else block in there for; it has no corresponding if? Arrays are easy to work with:
    PHP Code:
    if (!in_array($fileExtension, array('txt''php''pl''...'))
    {
        
    // good to go.


  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    global is not a good idea.
    What is that else block in there for; it has no corresponding if? Arrays are easy to work with:
    PHP Code:
    if (!in_array($fileExtension, array('txt''php''pl''...'))
    {
        
    // good to go.

    Hi
    I really do not understand the full function, other then what I did
    to exclude a single extension and a single file, problem is I need to
    do more then one of each, here is the full function.

    PHP Code:
    function CreateRefFile($dir,$level,$last,&$files$main_dir) {
        
    $dp opendir($dir);

        if (
    $dp !== FALSE) {
            while (
    false!=($file=readdir($dp)) && $level == $last) {
                if (
    $file!="." && $file!="..") {
                    
    $path = (substr($dir, -1) == '/') ? $dir.$file $dir "/" $file;

                    if (
    is_dir($path)) {
                        if (
    ExcludeDirectory($path$main_dir)) {
                            continue;
                        }

                        
    CreateRefFile($path,$level+1,$last+1,$files$main_dir); // uses recursion
            
    global $excludeExtexcludeFile;
                    } else {
                        if ((
    strcmp($file$excludeFile) == 0) ||
            (
    substr($file, -4) === $excludeExt)) // exclude this extension
                          
    continue;

                        
    $r = @stat($path);
                        
    $files[] = sprintf("%s,%d,%d,%d"$path$r[7], $r[9],substr(sprintf('%o', @fileperms($path)), -3));  // reads the file into an array
                    
    }
                }
            }
            
    closedir($dp);
        }

    Thanks
    Sonny
    Last edited by sonny; 10-24-2012 at 06:47 AM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    in_array is the key here. If you give it a collection, you need to search the collection to see if its in there.

    I'd probably use a DirectoryIterator in combination with FilterIterators for this. Its a lot easier to compare items on an item by item basis than worry about any type of recursion required.
    PHP Code:
    class FilterExtensionDisallow extends RecursiveFilterIterator
    {
        public function 
    accept()
        {
            
    $bResult true;
            
    $aDisallow = array('php');
            
    $cur parent::current();
            if ((
    $cur instanceof SplFileInfo) && $cur->isFile())
            {
                if (
    in_array($cur->getExtension(), $aDisallow))
                {
                    
    $bResult false;
                }
            }
            return 
    $bResult;
        }
    }

    class 
    FilterStartingWithPeriod extends RecursiveFilterIterator
    {
        public function 
    accept()
        {
            
    $bResult true;
            
    $cur parent::current();
            if (
    $cur instanceof SplFileInfo)
            {
                if (
    strpos($cur->getFilename(), '.') === 0)
                {
                    
    $bResult false;
                }
            }

            return 
    $bResult;
        }
    }

    $directory = new RecursiveDirectoryIterator(__DIR__ '/path');
    $directory = new FilterStartingWithPeriod($directory);
    $directory = new FilterExtensionDisallow($directory);
    $directory = new RecursiveIteratorIterator($directory);

    foreach (
    $directory AS $item)
    {
        
    printf("%s, %d, %d, %s" PHP_EOLrealpath($item->getPathname()), $item->getSize(), $item->getMTime(), substr(sprintf('%o'$item->getPerms()), -4));

    Filters are nice since you can stack'em.

  • #5
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Hi I am just trying to use those two vars below with an array instead of a single variable
    I am having trouble implementing that into the code below correctly.

    PHP Code:

    $exclude_file 
    = array('file1.php1','file2.php','test/file3.php'); //don't check these files
    $exclude_ext = array('.php','html'); //don't check these extensions

                      
    if ((strcmp($file$exclude_file) == 0) ||
                            (
    substr($file, -4) === $exclude_ext))

                          continue; 
    Thanks
    Sonny

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You cannot use strcmp with an array. You use in_array which returns a boolean:
    PHP Code:
    if (in_array($file$exclude_file) || in_array(substr($file, -4), $exclude_ext)) 
    Should do it.

  • Users who have thanked Fou-Lu for this post:

    sonny (10-25-2012)

  • #7
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You cannot use strcmp with an array. You use in_array which returns a boolean:
    PHP Code:
    if (in_array($file$exclude_file) || in_array(substr($file, -4), $exclude_ext)) 
    Should do it.
    Thanks will try that, looks a lot smother then what I was thinking about
    I was going to try and implement something like this in there somehow.

    PHP Code:
          foreach ($excludeFile as $ex)  {
            
    $exF $ex;
        echo 
    $exF;
        } 

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    foreach is a waste, you only use that if you need to iterate. Using array_search, in_array, or isset for offsets would be faster.
    I'd still recommend filters. They are easy to work with, but tricky if you wanted to make them dynamic (it appears to spawn a new instance of Iterator each recursive iteration, so you can't create a method like addIgnoreExtension to it without dealing at a static level). Filters stack, so I can say ignore .php, then later add one to ignore .css or any file starting with ., or add a Regex filter, or anything of the sorts. Super handy.

  • #9
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    foreach is a waste, you only use that if you need to iterate. Using array_search, in_array, or isset for offsets would be faster.
    I'd still recommend filters. They are easy to work with, but tricky if you wanted to make them dynamic (it appears to spawn a new instance of Iterator each recursive iteration, so you can't create a method like addIgnoreExtension to it without dealing at a static level). Filters stack, so I can say ignore .php, then later add one to ignore .css or any file starting with ., or add a Regex filter, or anything of the sorts. Super handy.
    if you say it, Its most likely the best way to go, but I can't code like you, I wish I knew a quarter of what you know, besides this is checking over 3000 files and is working fine

    when I try that in_array I get Wrong datatype for second argument
    could I just wrap a foreach like I posted above around that? and do both vars?

    Sonny

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, don't use a foreach as you have to evaluate every item in the array to the current iteration. Effectively bringing your magnitude up a lot.
    What is the actual error, it should tell you what the datatype is. If you have it as this:
    PHP Code:
    $exclude_file = array('file1.php1','file2.php','test/file3.php'); //don't check these files
    $exclude_ext = array('.php','html'); //don't check these extensions 
    if (in_array($file$exclude_file) || in_array(substr($file, -4), $exclude_ext)) 
    Then the datatypes should be fine, both exclude_file and exclude_ext are arrays.

  • #11
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    No, don't use a foreach as you have to evaluate every item in the array to the current iteration. Effectively bringing your magnitude up a lot.
    What is the actual error, it should tell you what the datatype is. If you have it as this:
    PHP Code:
    $exclude_file = array('file1.php1','file2.php','test/file3.php'); //don't check these files
    $exclude_ext = array('.php','html'); //don't check these extensions 
    if (in_array($file$exclude_file) || in_array(substr($file, -4), $exclude_ext)) 
    Then the datatypes should be fine, both exclude_file and exclude_ext are arrays.
    this is the complete error
    Warning: in_array() [function.in-array]: Wrong datatype for second argument

    Its this line below
    PHP Code:
    if (in_array($file$exclude_file) || in_array(substr($file, -4), $exclude_ext)) 

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yes, how is it relative to the two set variables though? Are they scope local variables, and are they created in the same branch?

  • #13
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yes, how is it relative to the two set variables though? Are they scope local variables, and are they created in the same branch?
    you mean do they contain anything? they are set as
    global $exclude_file, $exclude_ext; Yes

    Sonny

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Where have you set the exclude_file and exclude_ext.
    If they are set they can be a debugging nightmare (since they are globals). If it never works and applies an invalid type, then that indicates its never been set. But if it has been set anywhere before it became to scope, has worked and then stopped, that indicates something has modified them, and the only way to determine what is to debug it.

  • #15
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Where have you set the exclude_file and exclude_ext.
    If they are set they can be a debugging nightmare (since they are globals). If it never works and applies an invalid type, then that indicates its never been set. But if it has been set anywhere before it became to scope, has worked and then stopped, that indicates something has modified them, and the only way to determine what is to debug it.
    Now What?, your right, I moved those inside the function to test and it ran without error
    now I will see if it will exclude, that's odd I thought global always works? can I even set them as global vars? they run through about 5 functions to pass in.

    I will post back on how it went
    Thanks
    Sonny


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •