470,870 Members | 1,406 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

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!*
*

Jul 17 '05 #1
9 2433
>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?


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
Jul 17 '05 #2
Gordon Burditt wrote:
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?
I think there is a good chance you need to *UNESCAPE* the "'".


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.
Where did you get the value of $path?
From a readdir actually.
Are "magic quotes" on?


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.

Jul 17 '05 #3
Andrew DeFaria wrote:
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?


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
:)
Jul 17 '05 #4
Michael Austin wrote:
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...


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.
Jul 17 '05 #5
On Mon, 02 Aug 2004 13:07:07 GMT, Michael Austin <ma*****@firstdbasource.com>
wrote:
One thing that MS has really messed up is allowing spaces and certain
meta-characters in file names.


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 / <an**@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)
Jul 17 '05 #6
Andy Hassall wrote:
On Mon, 02 Aug 2004 13:07:07 GMT, Michael Austin <ma*****@firstdbasource.com>
wrote:

One thing that MS has really messed up is allowing spaces and certain
meta-characters in file names.

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


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
:)
Jul 17 '05 #7
Michael Austin wrote:
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...


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.
Jul 17 '05 #8
On Mon, 02 Aug 2004 15:45:04 -0700, Andrew DeFaria <An****@DeFaria.com> wrote:
Michael Austin wrote:
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...


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?


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 / <an**@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)
Jul 17 '05 #9
Andy Hassall wrote:
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.


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
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Roswitha Schoppe-Jantzen | last post: by
3 posts views Thread by Jeremy Shovan | last post: by
2 posts views Thread by Andrew | last post: by
4 posts views Thread by gnah | last post: by
5 posts views Thread by Markus Pitha | last post: by
6 posts views Thread by dmcglynn | last post: by
6 posts views Thread by MK | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.