#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2

    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"?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397

    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    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?..
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    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()
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    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.
    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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    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"
  16. #9
  17. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397

    I knew something was bugging me


    ...

    readdir() wants a resource, not a filename.

    Maybe you're thinking of scandir()? Or glob()?

    Comments on this post

    • f_razzoli agrees
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    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.
  20. #11
  21. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    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:
    PHP Code:
    opendir("C:\Users\tim"); 
    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.

    Comments on this post

    • f_razzoli agrees
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  22. #12
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    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); 
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    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.
  26. #14
  27. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,997
    Rep Power
    9397
    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).

IMN logo majestic logo threadwatch logo seochat tools logo