0

I have some folders that contain spaces, eg:

/user/mike/abc/de fg/hi

How do I escape the path so I can access it with php? (eg. filegetcontents glob, file_exists etc..) I tried:

$files = glob("/user/mike/abc/de\ fg/hi/*.*");
print_r($files)  //is empty

I also tried escapeshellcmd and escapeshellarg.

What am I doing wrong? (beside using files with spaces)

MilMike
  • 12,571
  • 15
  • 65
  • 82
  • @MichaelCoxon - in that post they replace the spaces with "\ " thats what I did (see my source) - doesn't work. (and it is not just for glob, I want to open the file that contains a space in all php functions) – MilMike Mar 14 '16 at 13:25
  • Because you are using the escape in a literal - it won't work properly - my guess is that it probably needs to be `"/de\\ fg/h"` since the escape will be *escaped*.. The other thing you can try is to use single quotes which I am pretty sure do not escape anything. ie.: `'/user/mike/abc/de\ fg/hi/*.*'` – Michael Coxon Mar 14 '16 at 13:29
  • @MichaelCoxon it works! - thanks! :) I tried it with single quotes, strange.. maybe some php settings. – MilMike Mar 14 '16 at 13:39
  • Not PHP settings.. that is a feature of PHP. :) – Michael Coxon Mar 14 '16 at 13:41
  • Maybe this could be useful: [**Is there a way to glob() only files?**](http://stackoverflow.com/q/14084378/4577762)... – FirstOne Mar 14 '16 at 13:42

1 Answers1

1

Because you are using the escape in a double-quote literal - it won't work properly - well, as intended.

It either needs to be "/user/mike/abc/de\\ fg/hi/*.*" since the escape will be escaped.

OR

You can use single quotes which do not escape anything and use the string exactly as you typed it. ie.: '/user/mike/abc/de\ fg/hi/*.*'

For further reading with other info about the things you can do with string literals see: http://php.net/manual/en/language.types.string.php

Michael Coxon
  • 5,311
  • 1
  • 24
  • 51