recursive delete all files/folders

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • betterdie@gmail.com

    recursive delete all files/folders

    Dear guru

    I want to delete all file and folder recursivly under php code, can
    anyone give me commend for this.

    Thank very much

  • Erwin Moller

    #2
    Re: recursive delete all files/folders

    betterdie@gmail .com wrote:
    [color=blue]
    > Dear guru
    >
    > I want to delete all file and folder recursivly under php code, can
    > anyone give me commend for this.
    >
    > Thank very much[/color]

    Hi,

    It is not too complicated.
    But pay attention to potential problems concerning file and directory
    permissions.
    Remember that PHP runs as user 'nobody' or 'apache' or 'www-data' on most
    *nix machines, and as IUSR_XXX on W$ machines/IIS.

    That user needs appropriate right to delete files and directories.

    For some reference and code-examples, start reading here:

    and


    You will probably use functions like rmdir() and unlink().

    Good luck!

    Regards,
    Erwin Moller

    Comment

    • Chung Leong

      #3
      Re: recursive delete all files/folders

      Something like the following should work. Untested though.

      function delete_dir($pat h) {
      $files = glob("$path/*");
      foreach($files as $file) {
      if(is_dir($file ) && !is_link($file) ) {
      delete_dir($fil e);
      }
      else {
      unlink($p);
      }
      }
      rmdir($path);
      }

      Comment

      • juglesh

        #4
        Re: recursive delete all files/folders


        deletes a file, or a folder and everything in it, whatever you put into
        $dirname.

        function rmdirr($dirname )
        {
        // Sanity check
        if (!file_exists($ dirname)) {
        echo "no directory by that name";
        return false;
        }

        // Simple delete for a file
        if (is_file($dirna me)) {
        return unlink($dirname );
        }

        // Loop through the folder
        $dir = dir($dirname);
        while (false !== $entry = $dir->read()) {
        // Skip pointers
        if ($entry == '.' || $entry == '..') {
        continue;
        }

        // Recurse
        rmdirr("$dirnam e/$entry");
        }// end while looping

        // Clean up
        $dir->close();
        return rmdir($dirname) ;

        }

        Comment

        • Chuck Anderson

          #5
          Re: recursive delete all files/folders

          betterdie@gmail .com wrote:
          [color=blue]
          >Dear guru
          >
          >I want to delete all file and folder recursivly under php code, can
          >anyone give me commend for this.
          >
          >Thank very much
          >
          >
          >[/color]
          I wrote a function to do that a few weeks ago that was based on a
          recursive copy function (for an application script on my local Windows
          machine), but when it came time to test it, I chickened out and just
          wrote a script to remove the specific folders used in my application.

          Be careful. Those files do not go to the recycle bin.

          --
          *************** **************
          Chuck Anderson • Boulder, CO

          Integrity is obvious.
          The lack of it is common.
          *************** **************

          Comment

          • Alexey Kulentsov

            #6
            Re: recursive delete all files/folders

            betterdie@gmail .com wrote:[color=blue]
            > Dear guru
            >
            > I want to delete all file and folder recursivly under php code, can
            > anyone give me commend for this.[/color]

            /// Clean directory
            /** Delete all files in directory
            * @param $path directory to clean
            * @param $recursive delete files in subdirs
            * @param $delDirs delete subdirs
            * @param $delRoot delete root directory
            * @access public
            * @return success
            */
            function cleanDir($path, $recursive=true ,$delDirs=false ,$delRoot=null)
            {
            $result=true;
            if($delRoot===n ull) $delRoot=$delDi rs;
            if(!$dir=@dir($ path)) return false;
            while($file=$di r->read())
            {
            if($file==='.' || $file==='..') continue;

            $full=$dir->path.DIRECTORY _SEPARATOR.$fil e;
            if(is_dir($full ) && $recursive)
            {
            $result&=filesy s::cleanDir($fu ll,$recursive,$ delDirs,$delDir s);
            }else if(is_file($ful l))
            {
            $result&=unlink ($full);
            }
            }
            $dir->close();
            if($delRoot)
            {
            $result&=rmdir( $dir->path);
            }
            return $result;
            }

            Comment

            Working...