1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved Delete files with certain file extensions

Discussion in 'Development' started by Kabluinc, Aug 9, 2017.

  1. Kabluinc

    Kabluinc Baby Zombie

    Messages:
    129
    Hi,

    Right now i am using the code below to delete all the files in a given directiory and delete the folder.

    PHP:



        
    /**
        * Recursively delete a directory
        *
        * @param string $dir
        *           Directory name
        * @param boolean $deleteRootToo
        *           Delete specified top-level directory as well
        */
        
    public function unlinkRecursive($dir$deleteRootToo) {
            if (! 
    $dh = @opendir $dir )) {
                return;
            }
            while ( 
    false !== ($obj readdir $dh )) ) {
                if (
    $obj == '.' || $obj == '..') {
                    continue;
                }
              
                if (! @
    unlink $dir '/' $obj )) {
                    
    $this->unlinkRecursive $dir '/' $objtrue );
                }
            }
          
            
    closedir $dh );
          
            if (
    $deleteRootToo) {
                @
    rmdir $dir );
            }
          
            return;

    }
    However i would like to know how, instead of deleting every file, how can i delete all the files that have the extension mcr forexample? that would mean that if the folder contained files with diffrent extensions. such as mcr, mca, mcapm etc, it would only delete all the mcr files.

    Weird question but please help me :)
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Use substr.
    PHP:
    function fileHasExtension(string $filestring $extension) : bool{
        return 
    substr($file, -strlen($extension)) === $extension;
    }
     
    Last edited: Aug 9, 2017
    Kabluinc likes this.
  3. Kabluinc

    Kabluinc Baby Zombie

    Messages:
    129
    Very grateful for your help :) however iv tried playing round with that bit of code in my one but i cant seem to get it to work. im not really sure what the right way is to add it :/

    this is what i tried

    PHP:
      /**
        * Recursively delete a directory
        *
        * @param string $dir
        *           Directory name
        * @param boolean $deleteRootToo
        *           Delete specified top-level directory as well
        */
        
    public function unlinkRecursive($dir$deleteRootToo) {
            if (! 
    $dh = @opendir $dir )) {
                return;
            }
            while ( 
    false !== ($obj readdir $dh )) ) {
                if (
    $obj == '.' || $obj == '..') {
                    continue;
                }
              
                if (! @
    unlink $dir '/' $obj )) {
    if(
    $this->fileHasExtension($obj"mcr")){
                    
    $this->unlinkRecursive $dir '/' $objtrue );
                }
            }
    }
          
            
    closedir $dh );
          
            if (
    $deleteRootToo) {
                @
    rmdir $dir );
            }
          
            return;

    }
    function 
    fileHasExtension(string $filestring $extension) : bool{
        return 
    substr($file0strlen($extension)) === $extension;
    }
    please help :)
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Hey, really sorry. I don't quite get along with some php functions. I've updated my snippet. Try it :)
     
    Kabluinc likes this.
  5. Kabluinc

    Kabluinc Baby Zombie

    Messages:
    129
    I just coudnt get around implementing it properly. I used this instead

    PHP:
    function unlink_recursive($dir_name$ext) {

        
    // Exit if there's no such directory
        
    if (!file_exists($dir_name)) {
            return 
    false;
        }

        
    // Open the target directory
        
    $dir_handle dir($dir_name);

        
    // Take entries in the directory one at a time
        
    while (false !== ($entry $dir_handle->read())) {

            if (
    $entry == '.' || $entry == '..') {
                continue;
            }

            
    $abs_name "$dir_name/$entry";

            if (
    is_file($abs_name) && preg_match("/^.+\.$ext$/"$entry)) {
                if (
    unlink($abs_name)) {
                    continue;
                }
                return 
    false;
            }

            
    // Recurse on the children if the current entry happens to be a "directory"
            
    if (is_dir($abs_name) || is_link($abs_name)) {
                
    unlink_recursive($abs_name$ext);
            }

        }

        
    $dir_handle->close();
        return 
    true;

    }
    from https://stackoverflow.com/questions/12869601/how-to-delete-all-txt-files-from-a-directory-using-php

    Thanks for your help though :)
     
    Last edited: Aug 9, 2017
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.