YakovL YakovL - 6 months ago 5x
PHP Question

Apache/PHP: file_put_contents using a Windows absolute path? (possible?)

For some reasons I'd like to have some data stored in a removable storage (namely USB-stick). I have the WAMP package installed and it has its .../www/ folder where scripts can do edit files etc. I'd like my server to be able to read/write files from/to another folder which is in the removable storage.

I've tried already relative paths and can see that

works with folders outside
(potentially a cause of security issues..). However, my removable storage is the
drive in Windows, so to write there I need to use an absolute path. I've tried to use the Windows absolute path directly like

file_put_contents("C:\existing\path\copied\from\explorer\new_file.txt","Stage 3, successfully written");

(first on
to distinct if a fail of writing occurs because the path doesn't work or because writing to another storage is forbidden) and that failed.

So, is it possible at all? What should I use instead of the path as it is written in Windows explorer? Or may be I should use another function to achieve this? Or give that path an some alias using Apache config?

The point is the removable storage should be safely removable at any time, except for the moment of reading/writing.

PS thanks to ÁlvaroGonzález, I've figured that I just forgot to escape the
in my absolute path, copied from explorer, that's why writing to it directly failed.


Just a tiny syntax problem:


... prints something like:

string(46) "C:xisting\path\copiedromxplorer

... because you've been unlucky enough to use a double-quoted string and existing escape sequences:

  • \e escape (hex 1B)
  • \f formfeed (hex 0C)
  • \n newline (hex 0A)

Some valid alternatives:

  • 'C:\existing\path\copied\from\explorer\new_file.txt'
  • "C:\\existing\\path\\copied\\from\\explorer\\new_file.txt"
  • "C:/existing/path/copied/from/explorer/new_file.txt"