Anyone Have A Better file_exists() ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Google Mike

    Anyone Have A Better file_exists() ?

    {NOTE: I have PHP 4.2.2 for RH9 Linux.}

    Anyone have a better file_exists() out there? Even if you use shell
    out tricks with Linux using the `command` trick, I'd be interested to
    see what you came up with.

    I have this remotely mounted SMB shares on Linux, stuck in my /mnt
    directory, and the docs tell me that file_exists() doesn't work on
    that. When I tested this, I found this to be true. Therefore, I need
    an alternative.
  • Anders K. Madsen

    #2
    Re: Anyone Have A Better file_exists() ?

    On 27 Jul 2004 13:25:24 -0700
    googlemike@hotp op.com (Google Mike) wrote:
    [color=blue]
    > {NOTE: I have PHP 4.2.2 for RH9 Linux.}
    >
    > Anyone have a better file_exists() out there? Even if you use shell
    > out tricks with Linux using the `command` trick, I'd be interested to
    > see what you came up with.
    >
    > I have this remotely mounted SMB shares on Linux, stuck in my /mnt
    > directory, and the docs tell me that file_exists() doesn't work on
    > that. When I tested this, I found this to be true. Therefore, I need
    > an alternative.[/color]

    It seems that:
    test -e <file>; echo $? works fine...

    I guess you could do something like:

    <?php

    // Array of files to check.
    $file = array(
    $_ENV['HOME'], // Should exist.
    $_ENV['HOME'] . "/jaiewrpfjaoiejf " // Should not exist.
    );

    // Command to run.
    $cmd = 'test -e';

    // "Just do it(r)"
    foreach ($file as $key => $val) {
    exec("$cmd $val", $dummy_array, $return);
    if ($return == 0) {
    echo "$key: $val exist!\n";
    } else {
    echo "$key: $val doesn't exist!\n";
    }
    }
    ?>

    NOTE: Remember that shell-programs return 0 on success and not-0 on
    failure, thus the if ($return == 0) { // success }

    'test -e' checks for anything in the filesystem (dirs, files, pipes,
    symlinks --- the whole lot), if you just want to check for files use
    'test -f' or see 'man test' for more info.

    Best regards
    Madsen

    --
    Anders K. Madsen --- http://lillesvin.linux.dk

    "There are 10 types of people in the world.
    Those who understand binary - and those who don't."

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

    iD8DBQFBBuSllNH Je/JASHcRAo22AJ9nL 2erMBnhJbuVffQY f0U9MGqDBgCfeb9 L
    XQ9ZOlqT9jva+qs Mplm3LfE=
    =Bcgl
    -----END PGP SIGNATURE-----

    Comment

    • steve

      #3
      Re: Anyone Have A Better file_exists() ?

      "Google Mike" wrote:[color=blue]
      > {NOTE: I have PHP 4.2.2 for RH9 Linux.}
      >
      > Anyone have a better file_exists() out there? Even if you use shell
      > out tricks with Linux using the `command` trick, I’d be
      > interested to
      > see what you came up with.
      >
      > I have this remotely mounted SMB shares on Linux, stuck in my /mnt
      > directory, and the docs tell me that file_exists() doesn’t work
      > on
      > that. When I tested this, I found this to be true. Therefore, I[/color]
      need[color=blue]
      > an alternative.[/color]

      Why would it not work, and where in the doc it says it does not work.
      I cannot find that in: http://ca2.php.net/file_exists

      --
      http://www.dbForumz.com/ This article was posted by author's request
      Articles individually checked for conformance to usenet standards
      Topic URL: http://www.dbForumz.com/PHP-file_exi...ict133885.html
      Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=447124

      Comment

      • Pjotr Wedersteers

        #4
        Re: Anyone Have A Better file_exists() ?

        Google Mike wrote:[color=blue]
        > {NOTE: I have PHP 4.2.2 for RH9 Linux.}
        >
        > Anyone have a better file_exists() out there? Even if you use shell
        > out tricks with Linux using the `command` trick, I'd be interested to
        > see what you came up with.
        >
        > I have this remotely mounted SMB shares on Linux, stuck in my /mnt
        > directory, and the docs tell me that file_exists() doesn't work on
        > that. When I tested this, I found this to be true. Therefore, I need
        > an alternative.[/color]

        What if you just try opening the file for reading ? Would that not yield a
        failure if the file is not present ?
        HTH
        Pjotr


        Comment

        • Anders K. Madsen

          #5
          Re: Anyone Have A Better file_exists() ?

          On Wed, 28 Jul 2004 15:15:15 +0200
          "Pjotr Wedersteers" <x33159@westert erp.com> wrote:
          [color=blue]
          > What if you just try opening the file for reading ? Would that not
          > yield a failure if the file is not present ?[/color]

          Yeah, it would, but it'd also be slower than necessary.

          --
          Anders K. Madsen --- http://lillesvin.linux.dk

          "There are 10 types of people in the world.
          Those who understand binary - and those who don't."

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

          iD8DBQFBB7D8lNH Je/JASHcRAm+mAJ92D ZghE3JNGzlhYTaG nPmpvpe27wCcCQ4 z
          kRSprvWN9WZoFo6 a3Hvp5PU=
          =6PQI
          -----END PGP SIGNATURE-----

          Comment

          • Pjotr Wedersteers

            #6
            Re: Anyone Have A Better file_exists() ?

            Anders K. Madsen wrote:[color=blue][color=green]
            >>On Wed, 28 Jul 2004 15:15:15 +0200
            >>"Pjotr Wedersteers" <x33159@westert erp.com> wrote:[/color][/color]
            [color=blue][color=green]
            >> What if you just try opening the file for reading ? Would that not
            >> yield a failure if the file is not present ?[/color][/color]
            [color=blue]
            > Yeah, it would, but it'd also be slower than necessary.[/color]

            --

            Well, I just put it to the test. You'd be surprised perhaps (I wasn't).

            Using your 'test -e' shell command execution testing for a files presence
            took just over 1.1 seconds on my P3-500 LAMP box.
            Using 'my' @fopen ($file,'r'); also a hundred times took 0.021 seconds. (@to
            suppress warnings)

            Why did you think executing a shell command would be faster ?
            Pjotr


            Comment

            • Anders K. Madsen

              #7
              Re: Anyone Have A Better file_exists() ?

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

              On Wed, 28 Jul 2004 18:01:19 +0200
              "Pjotr Wedersteers" <x33159@westert erp.com> wrote:
              [color=blue]
              > Anders K. Madsen wrote:[color=green][color=darkred]
              > >>On Wed, 28 Jul 2004 15:15:15 +0200
              > >>"Pjotr Wedersteers" <x33159@westert erp.com> wrote:[/color][/color]
              >[color=green][color=darkred]
              > >> What if you just try opening the file for reading ? Would that not
              > >> yield a failure if the file is not present ?[/color][/color]
              >[color=green]
              > > Yeah, it would, but it'd also be slower than necessary.[/color]
              >[/color]

              Please note that I didn't say "compared to the shell-way".
              [color=blue]
              >
              > Well, I just put it to the test. You'd be surprised perhaps (I
              > wasn't).
              >
              > Using your 'test -e' shell command execution testing for a files
              > presence took just over 1.1 seconds on my P3-500 LAMP box.
              > Using 'my' @fopen ($file,'r'); also a hundred times took 0.021
              > seconds. (@to suppress warnings)
              >
              > Why did you think executing a shell command would be faster ?[/color]

              I didn't actually... What I thought would be faster was file_exists().

              I'm not the least bit surprised that doing it through a shell is slower
              than opening an closing the file, since you'd have PHP invoking a shell
              to do it. (Might actually be faster if you do it through proc_open();)

              But my reasoning for thinking that file_exists() is faster than doing it
              with fopen() is that file_exists() doesn't have to open and close a
              file.

              Furthermore, it might also have impact whether or not the file actually
              exists. (I'll try testing it myself.)

              Best regards,
              Madsen

              - --
              Anders K. Madsen --- http://lillesvin.linux.dk

              "There are 10 types of people in the world.
              Those who understand binary - and those who don't."

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

              iD8DBQFBB+YXlNH Je/JASHcRAhWGAJ9gd 9xDHV6VoLAUjEzO zfOwTHUGtQCeOZ6 U
              kE1A8f/1RQMac5jUW+JcN+ I=
              =x0aO
              -----END PGP SIGNATURE-----

              Comment

              • Google Mike

                #8
                Re: Anyone Have A Better file_exists() ?

                "Anders K. Madsen" <madsen@lillesv in.linux.dk> wrote in message[color=blue]
                > googlemike@hotp op.com (Google Mike) wrote:
                >[color=green]
                > > {NOTE: I have PHP 4.2.2 for RH9 Linux.}
                > >
                > > Anyone have a better file_exists() out there? Even if you use shell
                > > out tricks with Linux using the `command` trick, I'd be interested to
                > > see what you came up with.
                > >
                > > I have this remotely mounted SMB shares on Linux, stuck in my /mnt
                > > directory, and the docs tell me that file_exists() doesn't work on
                > > that. When I tested this, I found this to be true. Therefore, I need
                > > an alternative.[/color]
                >
                > It seems that:
                > test -e <file>; echo $? works fine...
                >
                > I guess you could do something like:
                >
                > <?php
                >
                > // Array of files to check.
                > $file = array(
                > $_ENV['HOME'], // Should exist.
                > $_ENV['HOME'] . "/jaiewrpfjaoiejf " // Should not exist.
                > );
                >
                > // Command to run.
                > $cmd = 'test -e';
                >
                > // "Just do it(r)"
                > foreach ($file as $key => $val) {
                > exec("$cmd $val", $dummy_array, $return);
                > if ($return == 0) {
                > echo "$key: $val exist!\n";
                > } else {
                > echo "$key: $val doesn't exist!\n";
                > }
                > }
                > ?>
                >
                > NOTE: Remember that shell-programs return 0 on success and not-0 on
                > failure, thus the if ($return == 0) { // success }
                >
                > 'test -e' checks for anything in the filesystem (dirs, files, pipes,
                > symlinks --- the whole lot), if you just want to check for files use
                > 'test -f' or see 'man test' for more info.
                >
                > Best regards
                > Madsen[/color]

                I adapted your solution here and it works on Linux. Thank you very
                much.

                function exists_file($fi le) {
                return (`test -e "$file";ech o $?`==0);
                }

                (For newbies who read this, note that I used the backtick (`)
                operator.)

                Also, you should experiment and see what happens when you take off the
                ==0 and the () wrap. You'll be disappointed. You'll find that you have
                to use these to get the result you want.

                Comment

                • 2metre

                  #9
                  Re: Anyone Have A Better file_exists() ?

                  Google Mike wrote:
                  [color=blue]
                  > {NOTE: I have PHP 4.2.2 for RH9 Linux.}
                  >
                  > Anyone have a better file_exists() out there? Even if you use shell
                  > out tricks with Linux using the `command` trick, I'd be interested to
                  > see what you came up with.
                  >
                  > I have this remotely mounted SMB shares on Linux, stuck in my /mnt
                  > directory, and the docs tell me that file_exists() doesn't work on
                  > that. When I tested this, I found this to be true. Therefore, I need
                  > an alternative.[/color]

                  I use file_exists() on remote mounted files and it works.

                  Set-up:
                  Linux RH6.2, Samba 2.2.7

                  My remote mounted SMB shares are NOT mounted in /mnt.

                  The SMB shares are directories on a Windows XP Pro box.

                  Comment

                  • 2metre

                    #10
                    Re: Anyone Have A Better file_exists() ?

                    2metre wrote:
                    [color=blue]
                    > Set-up:
                    > Linux RH6.2, Samba 2.2.7[/color]

                    Correction: that was supposd to read RH7.2. PHP=v4.3.4

                    Test script was running as PHP as CGI.

                    My entire Web root is mounted from the Windows box because my preferred
                    version control is Microsoft Visual Source Safe.

                    Comment

                    • Google Mike

                      #11
                      Re: Anyone Have A Better file_exists() ?

                      "Pjotr Wedersteers" <x33159@westert erp.com> wrote in message[color=blue]
                      > Well, I just put it to the test. You'd be surprised perhaps (I wasn't).
                      >
                      > Using your 'test -e' shell command execution testing for a files presence
                      > took just over 1.1 seconds on my P3-500 LAMP box.
                      > Using 'my' @fopen ($file,'r'); also a hundred times took 0.021 seconds. (@to
                      > suppress warnings)
                      >
                      > Why did you think executing a shell command would be faster ?
                      > Pjotr[/color]

                      What if the file is 40MB? Does fopen operate more slowly than 0.021
                      seconds then?

                      I'm willing to use the @fopen() technique if your results show me that
                      it's faster in such a case. For now, I'm using the exists_file()
                      routine that I adapted from Madsen and posted last night.

                      Comment

                      Working...