deleting all the files in a directory

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

    deleting all the files in a directory

    Hello,

    Using PHP 4, what are the shortest amount of lines I can write to
    delete all the files in a given directory?

    Thanks for your help, - Dave

  • NC

    #2
    Re: deleting all the files in a directory

    laredotorn...@z ipmail.com wrote:[color=blue]
    >
    > Using PHP 4, what are the shortest amount of lines I can write to
    > delete all the files in a given directory?[/color]

    One, if you are willing to live with an OS-dependent solution. You can
    read your OS manual for the command that removes files (rm on Unix or
    del on Windows), figure out what comand-line keys are necessary to
    remove all files in a given directory, and pass the respective command
    to your operating system via exec(), passthru(), or system().

    Cheers,
    NC

    Comment

    • Ewoud Dronkert

      #3
      Re: deleting all the files in a directory

      laredotornado@z ipmail.com wrote:[color=blue]
      > Using PHP 4, what are the shortest amount of lines I can write to
      > delete all the files in a given directory?[/color]

      <?php $ret = `cd $mydir && rm -fr *`; ?>

      --
      E. Dronkert

      Comment

      • Andy Hassall

        #4
        Re: deleting all the files in a directory

        On 13 Feb 2006 12:14:40 -0800, laredotornado@z ipmail.com wrote:
        [color=blue]
        >Using PHP 4, what are the shortest amount of lines I can write to
        >delete all the files in a given directory?[/color]

        $dir = '/x/y/z';
        if ($dir = opendir($dir))
        {
        while (($file = readdir($dir)) !== false)
        {
        if (is_file("$dir/$file"))
        {
        unlink("$dir/$file");
        }
        }
        }

        If you really want it shorter you can remove some of the braces.

        --
        Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
        http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

        Comment

        • Ewoud Dronkert

          #5
          Re: deleting all the files in a directory

          Andy Hassall wrote:[color=blue]
          > if (is_file("$dir/$file"))[/color]

          You're potentially missing (symbolic) links and files larger than
          int-size. Better to check for '.' or '..' filenames. Clumsy, I know.

          --
          E. Dronkert

          Comment

          • Iván Sánchez Ortega

            #6
            Re: deleting all the files in a directory

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            Ewoud Dronkert wrote:
            [color=blue]
            > <?php $ret = `cd $mydir && rm -fr *`; ?>[/color]

            I can halve that:

            <?`rm -rf $mydir/*`;?>

            - --
            - ----------------------------------
            Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net


            Proudly running Debian Linux with 2.6.12-1-686 kernel, KDE3.5.0, and PHP
            5.1.2-1 generating this signature.
            Uptime: 23:58:50 up 2:28, 1 user, load average: 0.49, 0.75, 0.51

            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.2 (GNU/Linux)

            iD8DBQFD8Q/73jcQ2mg3Pc8RAq TyAJ4t6X+yGGbRp NAMfdpMBW/tG9ciLACfYRdK
            d6NfQdNGlgZAZWB JqhP/Ybg=
            =ttCu
            -----END PGP SIGNATURE-----

            Comment

            • Andy Hassall

              #7
              Re: deleting all the files in a directory

              On Mon, 13 Feb 2006 22:47:26 +0100, Ewoud Dronkert
              <firstname@last name.net.invali d> wrote:
              [color=blue]
              >Andy Hassall wrote:[color=green]
              >> if (is_file("$dir/$file"))[/color]
              >
              >You're potentially missing (symbolic) links[/color]

              Missing, or including? symlinks to files return true from is_file() if their
              target is a file, so yes, that should probably have a && !is_link() condition
              as well, depending on what the OP really wants deleted.
              [color=blue]
              >and files larger than int-size.[/color]

              Hm, didn't know about that one. http://bugs.php.net/bug.php?id=27792

              --
              Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
              http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

              Comment

              • Ewoud Dronkert

                #8
                Re: deleting all the files in a directory

                Andy Hassall wrote:[color=blue]
                > Missing, or including? symlinks to files return true from is_file() if their
                > target is a file, so yes, that should probably have a && !is_link() condition
                > as well, depending on what the OP really wants deleted.[/color]

                My guess is, not that but || is_link().

                --
                E. Dronkert

                Comment

                • Jim Michaels

                  #9
                  Re: deleting all the files in a directory


                  "Andy Hassall" <andy@andyh.co. uk> wrote in message
                  news:6ht1v1hcjo eo7p4n0o26erjpa s270j0k1g@4ax.c om...[color=blue]
                  > On 13 Feb 2006 12:14:40 -0800, laredotornado@z ipmail.com wrote:
                  >[color=green]
                  >>Using PHP 4, what are the shortest amount of lines I can write to
                  >>delete all the files in a given directory?[/color]
                  >[/color]
                  this works for one directory, but not subdirectories under it. for that, it
                  needs to be recursive.

                  function zapdir($dir) {
                  if ($dir = opendir($dir)) {
                  while (($file = readdir($dir)) !== false) {
                  if (is_file("$dir/$file")) {
                  unlink("$dir/$file");
                  }
                  //is a directory or a symlink
                  if (is_dir("$dir/$file") && "." != $file && ".." != $file) {
                  zapdir("$dir/$file");
                  rmdir("$dir/$file") or echo "unable to remove directory
                  $dir/$file\n";
                  }
                  if (is_link("$dir/$file")) {
                  unlink("$dir/$file"); //remove symbolic link. does not
                  remove original file AFAIK
                  }
                  }
                  }
                  }
                  zapdir('/x/y/z');

                  If you really want it shorter you can remove some of the braces.[color=blue]
                  >
                  > --
                  > Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
                  > http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool[/color]


                  Comment

                  • d

                    #10
                    Re: deleting all the files in a directory

                    "Jim Michaels" <jmichae3@nospa m.yahoo.com> wrote in message
                    news:kZGdnQVGF6 sV42zeRVn-pg@comcast.com. ..[color=blue]
                    >
                    > "Andy Hassall" <andy@andyh.co. uk> wrote in message
                    > news:6ht1v1hcjo eo7p4n0o26erjpa s270j0k1g@4ax.c om...[color=green]
                    >> On 13 Feb 2006 12:14:40 -0800, laredotornado@z ipmail.com wrote:
                    >>[color=darkred]
                    >>>Using PHP 4, what are the shortest amount of lines I can write to
                    >>>delete all the files in a given directory?[/color]
                    >>[/color]
                    > this works for one directory, but not subdirectories under it. for that,
                    > it needs to be recursive.
                    >
                    > function zapdir($dir) {
                    > if ($dir = opendir($dir)) {
                    > while (($file = readdir($dir)) !== false) {
                    > if (is_file("$dir/$file")) {
                    > unlink("$dir/$file");
                    > }
                    > //is a directory or a symlink
                    > if (is_dir("$dir/$file") && "." != $file && ".." != $file) {
                    > zapdir("$dir/$file");
                    > rmdir("$dir/$file") or echo "unable to remove directory
                    > $dir/$file\n";
                    > }
                    > if (is_link("$dir/$file")) {
                    > unlink("$dir/$file"); //remove symbolic link. does not
                    > remove original file AFAIK
                    > }
                    > }
                    > }
                    > }
                    > zapdir('/x/y/z');[/color]

                    That will run out of stack space in large directory trees... Best to close
                    the $dir before calling zapdir...
                    [color=blue]
                    > If you really want it shorter you can remove some of the braces.[color=green]
                    >>
                    >> --
                    >> Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
                    >> http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool[/color]
                    >
                    >[/color]


                    Comment

                    • Mladen Gogala

                      #11
                      Re: deleting all the files in a directory

                      On Mon, 13 Feb 2006 22:47:26 +0100, Ewoud Dronkert wrote:
                      [color=blue]
                      > You're potentially missing (symbolic) links and files larger than
                      > int-size. Better to check for '.' or '..' filenames. Clumsy, I know.[/color]

                      This will work better:

                      foreach (glob("/mydir/*") as $f) {
                      filetype($f)== 'file' ? unlink($f) : null;
                      }

                      If my coding style betrays using some other scripting languages
                      as well, I cannot help it. In some other scripting languages that
                      might have been coded like this:

                      foreach (glob "/mydir/*") {
                      unlink($_) if -f $_;
                      }

                      As I don't have "suffix if" in PHP, I came up with the ternary operator.
                      Note that I could have written '' instead of "null" and still have the
                      same effect. Of course, I cannot rewrite something like this:

                      map { unlink($_) if -f $_; } glob("/mydir/*");



                      --


                      Comment

                      • Mladen Gogala

                        #12
                        Re: deleting all the files in a directory

                        On Mon, 13 Feb 2006 21:34:28 +0100, Ewoud Dronkert wrote:
                        [color=blue]
                        > <?php $ret = `cd $mydir && rm -fr *`; ?>[/color]

                        That will be a little harder on a Windows system.

                        --


                        Comment

                        • Mladen Gogala

                          #13
                          Re: deleting all the files in a directory

                          On Mon, 13 Feb 2006 21:14:36 +0000, Andy Hassall wrote:
                          [color=blue]
                          > if ($dir = opendir($dir))
                          > {
                          > while (($file = readdir($dir)) !== false)[/color]

                          PHP supports globs, just as Perl does.

                          --


                          Comment

                          • Ewoud Dronkert

                            #14
                            Re: deleting all the files in a directory

                            Mladen Gogala wrote:[color=blue]
                            > PHP supports globs, just as Perl does.[/color]

                            Yeah, I'm always forgetting. Thanks.

                            --
                            E. Dronkert

                            Comment

                            • Bart the bear

                              #15
                              Re: deleting all the files in a directory


                              Mladen Gogala wrote:
                              [color=blue]
                              > map { unlink($_) if -f $_; } glob("/mydir/*");[/color]

                              Speaking of this, is there any chance of references to functions in
                              PHP?
                              The "{ unlink($_) if -f $_; }" part of the code here is so called
                              "anonymous
                              subroutine" or "subroutine reference" which is sort of convenient if
                              you don't
                              want to clutter the namespace with unneeded function names. All you
                              need
                              to be able to implement your one-liner in PHP is an anonymous function
                              reference
                              to pass to array_walk, which is identical to Perl's "map".
                              Also, Perl has qr() construct which compiles regular expressions and
                              makes
                              things significantly faster. Is there any flag to compile regular
                              expression in
                              prreg_match? The only documented flag here is to capture offset. If I
                              need to
                              go through multiple lines in a loop, will PHP compile the regular
                              expression?
                              The situation I have in mind is something like this:

                              while ( $line=fread($fi le,128)) {
                              if preg_match("/$regexp/",$line,$found) ....
                              }

                              As you can see, I'm a beginner with PHP. I have 5 years or so
                              experience
                              with Perl.

                              Comment

                              Working...