Connecting Tech Pros Worldwide Help | Site Map

opendir with dir with odd characters

Andrew DeFaria
Guest
 
Posts: n/a
#1: Jul 17 '05
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
Guest
 
Posts: n/a
#2: Jul 17 '05

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
Andrew DeFaria
Guest
 
Posts: n/a
#3: Jul 17 '05

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.escapeshellarg.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.

Michael Austin
Guest
 
Posts: n/a
#4: Jul 17 '05

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
:)
Andrew DeFaria
Guest
 
Posts: n/a
#5: Jul 17 '05

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.
Andy Hassall
Guest
 
Posts: n/a
#6: Jul 17 '05

re: opendir with dir with odd characters


On Mon, 02 Aug 2004 13:07:07 GMT, Michael Austin <maustin@firstdbasource.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\004\005\006\a\b\t\n\v\f\r\016\017\020 \021\022\023\024\025\026\027\030\031\032\033\034\0 35\036\037\
!"#$%&'()\*+,-.0123456789:;<\=>?\@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{\|}~\177\200\201\202 \203\204\205\206\207\210\211\212\213\214\215\216\2 17\220\221\222\223\224\225\226\227\230\231\232\233 \234\235\236\237\240\241\242\243\244\245\246\247\2 50\251\252\253\254\255\256\257\260\261\262\263\264 \265\266\267\270\271\272\273\274\275\276\277\300\3 01\302\303\304\305\306\307\310\311\312\313\314\315 \316\317\320\321\322\323\324\325\326\327\330\331\3 32\333\334\335\336\337\340\341\342\343\344\345\346 \347\350\351\352\353\354\355\356\357\360\361\362\3 63\364\365\366\367\370\371\372\373\374\375\376\377

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
(v1.4.0 new 1st Aug 2004)
Michael Austin
Guest
 
Posts: n/a
#7: Jul 17 '05

re: opendir with dir with odd characters


Andy Hassall wrote:[color=blue]
> On Mon, 02 Aug 2004 13:07:07 GMT, Michael Austin <maustin@firstdbasource.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\004\005\006\a\b\t\n\v\f\r\016\017\020 \021\022\023\024\025\026\027\030\031\032\033\034\0 35\036\037\
> !"#$%&'()\*+,-.0123456789:;<\=>?\@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{\|}~\177\200\201\202 \203\204\205\206\207\210\211\212\213\214\215\216\2 17\220\221\222\223\224\225\226\227\230\231\232\233 \234\235\236\237\240\241\242\243\244\245\246\247\2 50\251\252\253\254\255\256\257\260\261\262\263\264 \265\266\267\270\271\272\273\274\275\276\277\300\3 01\302\303\304\305\306\307\310\311\312\313\314\315 \316\317\320\321\322\323\324\325\326\327\330\331\3 32\333\334\335\336\337\340\341\342\343\344\345\346 \347\350\351\352\353\354\355\356\357\360\361\362\3 63\364\365\366\367\370\371\372\373\374\375\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
:)
Andrew DeFaria
Guest
 
Posts: n/a
#8: Jul 17 '05

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.
Andy Hassall
Guest
 
Posts: n/a
#9: Jul 17 '05

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.andyhsoftware.co.uk/space> Space: disk usage analysis tool
(v1.4.0 new 1st Aug 2004)
Andrew DeFaria
Guest
 
Posts: n/a
#10: Jul 17 '05

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
Closed Thread


Similar PHP bytes