opendir with dir with odd characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew DeFaria

    opendir with dir with odd characters

    I'm attempting to put my music collection on the web using PHP and I hit
    a problem. When I attempt an opendir of a directory that contains a "'"
    character the opendir fails. Here's the code snippet:

    function GetAlbumArt ($path) {
    if ($handle = opendir ("$path")) {
    while (false !== ($element = readdir ($handle))) {
    if (strpos ($element, "AlbumArt") == 0 &&
    strpos ($element, "Large") != 0) {
    return $element;
    } // if
    } // while

    closedir ($handle);
    } // if
    } // GetAlbumArt

    $path = "/web/Music/Rock/David Lee Roth/A Little Ain\'t Enough"

    The error message I get on the page is:

    *Warning*: opendir(/web/Music/Rock/David Lee Roth/A Little Ain\'t
    Enough): failed to open dir: No such file or directory in
    */web/php/music.php* on line *6

    *I guess I need to escape the "'" but it seems like it's already escaped
    in the error message. How can I solve this problem?
    --
    Taxation with representation isn't so hot, either!*
    *

  • Gordon Burditt

    #2
    Re: opendir with dir with odd characters

    >I'm attempting to put my music collection on the web using PHP and I hit[color=blue]
    >a problem. When I attempt an opendir of a directory that contains a "'"
    >character the opendir fails. Here's the code snippet:
    >
    >function GetAlbumArt ($path) {
    > if ($handle = opendir ("$path")) {
    > while (false !== ($element = readdir ($handle))) {
    > if (strpos ($element, "AlbumArt") == 0 &&
    > strpos ($element, "Large") != 0) {
    > return $element;
    > } // if
    > } // while
    >
    > closedir ($handle);
    > } // if
    >} // GetAlbumArt
    >
    >$path = "/web/Music/Rock/David Lee Roth/A Little Ain\'t Enough"
    >
    >The error message I get on the page is:
    >
    >*Warning*: opendir(/web/Music/Rock/David Lee Roth/A Little Ain\'t
    >Enough): failed to open dir: No such file or directory in
    >*/web/php/music.php* on line *6
    >
    >*I guess I need to escape the "'" but it seems like it's already escaped
    >in the error message. How can I solve this problem?[/color]

    I think there is a good chance you need to *UNESCAPE* the "'".
    Where did you get the value of $path? Are "magic quotes" on?

    Gordon L. Burditt

    Comment

    • Andrew DeFaria

      #3
      Re: opendir with dir with odd characters

      Gordon Burditt wrote:
      [color=blue][color=green]
      >> I'm attempting to put my music collection on the web using PHP and I hit
      >> a problem. When I attempt an opendir of a directory that contains a "'"
      >> character the opendir fails. Here's the code snippet:
      >>
      >> function GetAlbumArt ($path) {
      >> if ($handle = opendir ("$path")) {
      >> while (false !== ($element = readdir ($handle))) {
      >> if (strpos ($element, "AlbumArt") == 0 &&
      >> strpos ($element, "Large") != 0) {
      >> return $element;
      >> } // if
      >> } // while
      >>
      >> closedir ($handle);
      >> } // if
      >> } // GetAlbumArt
      >>
      >> $path = "/web/Music/Rock/David Lee Roth/A Little Ain\'t Enough"
      >>
      >> The error message I get on the page is:
      >>
      >> *Warning*: opendir(/web/Music/Rock/David Lee Roth/A Little Ain\'t
      >> Enough): failed to open dir: No such file or directory in
      >> */web/php/music.php* on line *6
      >>
      >> *I guess I need to escape the "'" but it seems like it's already
      >> escaped in the error message. How can I solve this problem?[/color]
      >
      > I think there is a good chance you need to *UNESCAPE* the "'".[/color]

      I see no unescape function. I do see escapeshellarg
      <http://www.php.net/manual/en/function.escape shellarg.php>. Wonder if I
      should use that?... Nah doesn't work. Tried removing the "\" before the
      "'". Didn't work.
      [color=blue]
      > Where did you get the value of $path?[/color]

      From a readdir actually.
      [color=blue]
      > Are "magic quotes" on?[/color]

      What are "magic quotes"? How do I tell if they are turned on? If I need
      them then how do I turn them on?

      --
      God must love stupid people... He made so many.

      Comment

      • Michael Austin

        #4
        Re: opendir with dir with odd characters

        Andrew DeFaria wrote:
        [color=blue]
        > I'm attempting to put my music collection on the web using PHP and I hit
        > a problem. When I attempt an opendir of a directory that contains a "'"
        > character the opendir fails. Here's the code snippet:
        >
        > function GetAlbumArt ($path) {
        > if ($handle = opendir ("$path")) {
        > while (false !== ($element = readdir ($handle))) {
        > if (strpos ($element, "AlbumArt") == 0 &&
        > strpos ($element, "Large") != 0) {
        > return $element;
        > } // if
        > } // while
        >
        > closedir ($handle);
        > } // if
        > } // GetAlbumArt
        >
        > $path = "/web/Music/Rock/David Lee Roth/A Little Ain\'t Enough"
        >
        > The error message I get on the page is:
        >
        > *Warning*: opendir(/web/Music/Rock/David Lee Roth/A Little Ain\'t
        > Enough): failed to open dir: No such file or directory in
        > */web/php/music.php* on line *6
        >
        > *I guess I need to escape the "'" but it seems like it's already escaped
        > in the error message. How can I solve this problem?[/color]

        One thing that MS has really messed up is allowing spaces and certain
        meta-characters in file names. The BEST solution is to not store filenames with
        certain meta-charcters such as <space> single or double quote etc...


        --
        Michael Austin.
        Consultant - Available.
        Donations welcomed. Http://www.firstdbasource.com/donations.html
        :)

        Comment

        • Andrew DeFaria

          #5
          Re: opendir with dir with odd characters

          Michael Austin wrote:
          [color=blue]
          > One thing that MS has really messed up is allowing spaces and certain
          > meta-characters in file names. The BEST solution is to not store
          > filenames with certain meta-charcters such as <space> single or double
          > quote etc...[/color]

          What are you talking about?!? Unix has allowed spaces and yes even
          things like "'" and "&" as well as unprintable characters in filenames
          for years! Granted it's not commonly used but it is indeed allowed.

          In any event this does not solve my problem. Again, remember, I am
          webifying my album collection. As such there are often spaces and other
          such characters in both directory names and file names. Note that this
          is a directory name that has this problem. Are you saying that PHP is
          incapable of opendir'ing a directory that contains a "'" or "&"?!?

          --
          It may be that your sole purpose in life is simply to serve as a warning
          to others.

          Comment

          • Andy Hassall

            #6
            Re: opendir with dir with odd characters

            On Mon, 02 Aug 2004 13:07:07 GMT, Michael Austin <maustin@firstd basource.com>
            wrote:
            [color=blue]
            >One thing that MS has really messed up is allowing spaces and certain
            >meta-characters in file names.[/color]

            Unix filesystems are typically MORE liberal than FAT32 and NTFS with regards
            to filenames; the only invalid characters are chr(0) (NUL) and the directory
            separator /.

            andyh@server:~/tmp$ cat makeIckyFile.pl
            #!/usr/bin/perl
            my $fn;
            $fn = join '', grep {$_ ne '/'} map {chr} (1..255);
            open my $fp, '>', "tmp/$fn"
            or die $!;

            andyh@server:~/tmp$ ./makeIckyFile.pl
            andyh@server:~/tmp$ ls -l tmp/
            total 0
            -rw-r--r-- 1 andyh users 0 Aug 2 21:03
            \001\002\003\00 4\005\006\a\b\t \n\v\f\r\016\01 7\020\021\022\0 23\024\025\026\ 027\030\031\032 \033\034\035\03 6\037\
            !"#$%&'()\*+ ,-.0123456789:;<\ =>?\@ABCDEFGHIJ KLMNOPQRSTUVWXY Z[\\]^_`abcdefghijkl mnopqrstuvwxyz{ \|}~\177\200\20 1\202\203\204\2 05\206\207\210\ 211\212\213\214 \215\216\217\22 0\221\222\223\2 24\225\226\227\ 230\231\232\233 \234\235\236\23 7\240\241\242\2 43\244\245\246\ 247\250\251\252 \253\254\255\25 6\257\260\261\2 62\263\264\265\ 266\267\270\271 \272\273\274\27 5\276\277\300\3 01\302\303\304\ 305\306\307\310 \311\312\313\31 4\315\316\317\3 20\321\322\323\ 324\325\326\327 \330\331\332\33 3\334\335\336\3 37\340\341\342\ 343\344\345\346 \347\350\351\35 2\353\354\355\3 56\357\360\361\ 362\363\364\365 \366\367\370\37 1\372\373\374\3 75\376\377

            --
            Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
            <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
            (v1.4.0 new 1st Aug 2004)

            Comment

            • Michael Austin

              #7
              Re: opendir with dir with odd characters

              Andy Hassall wrote:[color=blue]
              > On Mon, 02 Aug 2004 13:07:07 GMT, Michael Austin <maustin@firstd basource.com>
              > wrote:
              >
              >[color=green]
              >>One thing that MS has really messed up is allowing spaces and certain
              >>meta-characters in file names.[/color]
              >
              >
              > Unix filesystems are typically MORE liberal than FAT32 and NTFS with regards
              > to filenames; the only invalid characters are chr(0) (NUL) and the directory
              > separator /.
              >
              > andyh@server:~/tmp$ cat makeIckyFile.pl
              > #!/usr/bin/perl
              > my $fn;
              > $fn = join '', grep {$_ ne '/'} map {chr} (1..255);
              > open my $fp, '>', "tmp/$fn"
              > or die $!;
              >
              > andyh@server:~/tmp$ ./makeIckyFile.pl
              > andyh@server:~/tmp$ ls -l tmp/
              > total 0
              > -rw-r--r-- 1 andyh users 0 Aug 2 21:03
              > \001\002\003\00 4\005\006\a\b\t \n\v\f\r\016\01 7\020\021\022\0 23\024\025\026\ 027\030\031\032 \033\034\035\03 6\037\
              > !"#$%&'()\*+ ,-.0123456789:;<\ =>?\@ABCDEFGHIJ KLMNOPQRSTUVWXY Z[\\]^_`abcdefghijkl mnopqrstuvwxyz{ \|}~\177\200\20 1\202\203\204\2 05\206\207\210\ 211\212\213\214 \215\216\217\22 0\221\222\223\2 24\225\226\227\ 230\231\232\233 \234\235\236\23 7\240\241\242\2 43\244\245\246\ 247\250\251\252 \253\254\255\25 6\257\260\261\2 62\263\264\265\ 266\267\270\271 \272\273\274\27 5\276\277\300\3 01\302\303\304\ 305\306\307\310 \311\312\313\31 4\315\316\317\3 20\321\322\323\ 324\325\326\327 \330\331\332\33 3\334\335\336\3 37\340\341\342\ 343\344\345\346 \347\350\351\35 2\353\354\355\3 56\357\360\361\ 362\363\364\365 \366\367\370\37 1\372\373\374\3 75\376\377
              >[/color]

              You are of course correct, for whatever reason I had another filesystem on my
              brain - RMS.

              "icky" is a mild understatement for a description of that filename...

              --
              Michael Austin.
              Consultant - Available.
              Donations welcomed. Http://www.firstdbasource.com/donations.html
              :)

              Comment

              • Andrew DeFaria

                #8
                Re: opendir with dir with odd characters

                Michael Austin wrote:
                [color=blue]
                > You are of course correct, for whatever reason I had another
                > filesystem on my brain - RMS.
                >
                > "icky" is a mild understatement for a description of that filename...[/color]

                OK, with that out of our collective systems, let's get back to the
                problem at hand: How does one perform a successful operdir on a path
                name with one of them 'thar "icky" characters in PHP?
                --
                Hermits have no peer pressure.

                Comment

                • Andy Hassall

                  #9
                  Re: opendir with dir with odd characters

                  On Mon, 02 Aug 2004 15:45:04 -0700, Andrew DeFaria <Andrew@DeFaria .com> wrote:
                  [color=blue]
                  >Michael Austin wrote:
                  >[color=green]
                  >> You are of course correct, for whatever reason I had another
                  >> filesystem on my brain - RMS.
                  >>
                  >> "icky" is a mild understatement for a description of that filename...[/color]
                  >
                  >OK, with that out of our collective systems, let's get back to the
                  >problem at hand: How does one perform a successful operdir on a path
                  >name with one of them 'thar "icky" characters in PHP?[/color]

                  You just use opendir with a plain, unescaped version of the directory name.
                  Just tried it successfully on a directory with the same unspeakably icky name
                  as in the example in the prior post, and it worked fine.

                  Another poster pointed out magic_quotes, which looks like a likely culprit,
                  since presumably your directory doesn't have the \ in its name. magic_quotes
                  basically automatically escapes data from external sources, and IMHO causes
                  more trouble than it solves.

                  Use phpinfo() to find your current configuration, and change it in php.ini to
                  disable it. Or use stripslashes() to reverse its effects, if you can't disable
                  it.

                  --
                  Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
                  <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
                  (v1.4.0 new 1st Aug 2004)

                  Comment

                  • Andrew DeFaria

                    #10
                    Re: opendir with dir with odd characters

                    Andy Hassall wrote:
                    [color=blue]
                    > You just use opendir with a plain, unescaped version of the directory
                    > name. Just tried it successfully on a directory with the same
                    > unspeakably icky name as in the example in the prior post, and it
                    > worked fine.
                    >
                    > Another poster pointed out magic_quotes, which looks like a likely
                    > culprit, since presumably your directory doesn't have the \ in its
                    > name. magic_quotes basically automatically escapes data from external
                    > sources, and IMHO causes more trouble than it solves.
                    >
                    > Use phpinfo() to find your current configuration, and change it in
                    > php.ini to disable it. Or use stripslashes() to reverse its effects,
                    > if you can't disable it.[/color]

                    You're probably right about those magic quotes. However, in the meantime
                    I found a PHP package that does exactly what I was trying to do way
                    better than I could hope to do without a long time of coding.

                    Thanks though.

                    --
                    Backup not found: (A)bort (R)etry (P)anic

                    Comment

                    Working...