The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Readdir() and paths with spaces
Discuss Readdir() and paths with spaces in the PHP Development forum on Dev Shed. Readdir() and paths with spaces PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 24th, 2013, 05:27 PM
|
|
Contributing User
|
|
Join Date: Jan 2013
Location: Italy
Posts: 36
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
|
|
|
Readdir() and paths with spaces
On Windows, this returns an error:
readdir(escapeshellargs('C:\dir name with spaces'));
This doesn't happen with exec() or similar functions, but apparently readdir() handles paths with spaces in a different way.
How can I open an handler to "C:\dir name with spaces"?
|

January 24th, 2013, 05:51 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
12k posts!
You're not using the path as an argument to a shell command. Don't escapeshellargs() the path.
Last edited by requinix : January 24th, 2013 at 06:33 PM.
|

January 24th, 2013, 05:58 PM
|
|
Contributing User
|
|
Join Date: Jan 2013
Location: Italy
Posts: 36
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by requinix You're not using the path as an argument to a shell command. Don't escapeshellargs() the path. |
However, the output of escapeshellargs() is something like:
"C:\path with spaces"
, which seemed to me the right syntax... but it doesn't work.
What am I missing?..
|

January 24th, 2013, 06:32 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
It put quotes around the value.
|

January 24th, 2013, 06:38 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
escapeshellargs is for escaping shell arguments. only use it for that.
mysql_real_escape_string is for escaping mysql strings. Only use it for that.
htmlentities is for encoding HTML output. Only use it for that.
There's a pattern here.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

January 24th, 2013, 06:49 PM
|
|
Contributing User
|
|
Join Date: Jan 2013
Location: Italy
Posts: 36
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by requinix It put quotes around the value. |
That's what I wrote before:
"C:\path with spaces"
and also:
c:\"path with spaces"
c:\"path with spaces"\
"c:\path with spaces\"
All these work in the DOS prompt, but none of them work as a readdir() parameter. Maybe it's a PHP bug? But even so, is there a workaround?
I agree, I'm guilty I shouldnt even think about using escapeshellargs(), but please let's talk about the right syntax to use with readdir() 
|

January 24th, 2013, 06:52 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Dude, seriously, stop adding extra stuff to your variable. The argument to readdir is the name of the directory. Done, end of sentence. Nothing is mentioned about escaping, adding quotes, adding slashes, encoding, encrypting, fixing spaces, or anything like that. Just the directory. Take your code. Remove escapeshellargs. It is unnecessary and incorrect in this context.
|

January 24th, 2013, 07:03 PM
|
|
Contributing User
|
|
Join Date: Jan 2013
Location: Italy
Posts: 36
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by ManiacDan Dude, seriously, stop adding extra stuff to your variable. The argument to readdir is the name of the directory. Done, end of sentence. Nothing is mentioned about escaping, adding quotes, adding slashes, encoding, encrypting, fixing spaces, or anything like that. Just the directory. Take your code. Remove escapeshellargs. It is unnecessary and incorrect in this context. |
PLEASE, trust me: i removed escapeshellargs
I'm not trying to do anything unnecessary. Please, read carefully: I need to pass an absolute path to readdir(). The reason why I must do it is a long story. However it perfectly works if there are no spaces. BUT, I can't find a way to pass a path with spaces. This is a real problem, because I have clients, and they don't pay me to say "you are an idiot because you have spaces in your dirs".
I swear that if you find ANY alternatives to this:
readdir('c:\dir with spaces')
I will like it a lot, not matter if you use thousands quotes or a single char.
Really, this is not about style or geekism, or anything else: I am really trying to find a way to read "c:\dir with spaces"
|

January 24th, 2013, 07:28 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
I knew something was bugging me
...
readdir() wants a resource, not a filename.
Maybe you're thinking of scandir()? Or glob()?
|

January 25th, 2013, 09:02 AM
|
|
Contributing User
|
|
Join Date: Jan 2013
Location: Italy
Posts: 36
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by requinix ...
readdir() wants a resource, not a filename.
Maybe you're thinking of scandir()? Or glob()? |
No, actually the mistake is in the post and not in the code: I used $dp = opendir($dir);
but if $dir contained spaces, I always received an error. Quoting the spaces like you do in the prompt was useless.
I think that this only happens on Windows (at least on WinXP), as on Linux it worked as expected.
Since my script MUST work on Windows, I wrote a trivial Python script and called it from PHP. If there was a better alternative, I wasn't able to find it fast enough.
|

January 25th, 2013, 06:33 PM
|
 |
Lost in code
|
|
|
|
If you're hard-coding the paths in a PHP string, remember that backslash is not only the directory separate on windows, but also the escape character for a PHP string. So if you have something like this:
You will actually be injecting a tab character into the path, which will obviously fail.
However, remember that if you already have a string that contains a literal '\t' (not a tab character) you do not need to do any sort of replacing or escaping before passing it to opendir.
I know this has nothing to do specifically with spaces; but it's possible that the problem was misdiagnosed as being related to spaces.
|

January 25th, 2013, 07:58 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Oreo might be right, you should use the DIRECTORY_SEPARATOR constant then.
PHP Code:
$path = 'C:' . DIRECTORY_SEPARATOR
. 'Users' . DIRECTORY_SEPARATOR
. 'SomeGuy' . DIRECTORY_SEPARATOR
. 'Documents and Settings' . DIRECTORY_SEPARATOR
. 'Desktop' . DIRECTORY_SEPARATOR
. 'file.txt';
opendir($path);
|

January 25th, 2013, 07:59 PM
|
|
Contributing User
|
|
Join Date: Jan 2013
Location: Italy
Posts: 36
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by E-Oreo
I know this has nothing to do specifically with spaces; but it's possible that the problem was misdiagnosed as being related to spaces. |
I understand why you say this, and I agree.
Unfortunately, in my case none of the following worked:
$dp = opendir('C:\Programmi\x db');
$dp = opendir('"C:\Programmi\x db"');
$dp = opendir('C:\Programmi\"x db"');
However, I realized that this only happens on Windows (XP... didn't try other versions). On Linux, the first line works. So I beilive that this is a bug of PHP on Windows.
I found a horrible workaround (I invoke a Python script using exec() function), but if you have other suggestions to do this in PHP, I'll be grateful.
|

January 25th, 2013, 08:28 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
The first one should work, the other two will not.
Can you post the exact error message you're getting too? Maybe something else is going on. Also check that
PHP Code:
print_r(glob('C:\*'));
print_r(glob('C:\Programmi\*'));
var_dump(file_exists('C:\Programmi\x db'));
show 1) Programmi in the array, 2) "x db" in the array, and 3) bool(true).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|