2

I know there're plenty of topics regarding escaping characters but I just can't find the solution for my problem.

It's very easy. This is string I have:

$path = "C:\Users\Me\Desktop\14409238.jpg";

Howver, no matter how many escaping techniques I use, I can't manage to display the correct path without destroying it. In all cases the \14 will be replaced with

C:\Users\Me\Desktopd09238.jpg

How do I solve this?

thaikolja
  • 431
  • 3
  • 13

3 Answers3

3

try to change, the Physical path to access the image, stored on Desktop can be written as,

$path = "C:\Users\Me\Desktop\14409238.jpg";

to

$path = "C:\\Users\\Me\\Desktop\\14409238.jpg";
  • Thanks for your reply. However, I had forgotten that I do not have control over what $path gives me. And well, it does give me those annoying backslashes. – thaikolja Jul 28 '15 at 21:01
  • 1
    Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Aug 24 '15 at 20:04
3

Don't use backslashes in PHP for windows paths. It's smart enough to convert for you:

$path = "c:/users/me/desktop/...";

Using backslashes runs into the exact problem you have - backslashing certain characters turns them into metacharacters, not regular characters.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

Avoid the situation entirely, PHP under Windows allows you to submit paths with the backslash

c://Users/Me/Desktop/file.jpg

This also avoids interoperability headaches when a script must run within .nix and Windows.

Michael Morris
  • 531
  • 5
  • 7