0

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 file_put_contents works with folders outside www/ (potentially a cause of security issues..). However, my removable storage is the E:\ 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 C:\ 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 \ symbols in my absolute path, copied from explorer, that's why writing to it directly failed.

YakovL
  • 7,557
  • 12
  • 62
  • 102
  • Possible duplicate of [accessing file of another drive](http://stackoverflow.com/questions/10311674/accessing-file-of-another-drive) – I wrestled a bear once. Apr 19 '16 at 14:00
  • Its dangerous adding an Alias to a removeable drive, as if it is not there Apache will not start – RiggsFolly Apr 19 '16 at 14:04
  • 2
    I use absolute paths on every single PHP project I develop on Windows. They work flawlessly. In fact, they work better than in Windows itself because I can use both `/` and `\\` as directory separator. Can you please edit the question and explain *what code* fails and *how exactly* it fails? If you get a blank screen, please find out how to configure PHP to display error messages. – Álvaro González Apr 19 '16 at 14:21
  • @ÁlvaroGonzález thanks, you are right. I copied the path from explorer without escaping backslashes. I think, it's a better solution then using symlinks. You can answer the question, too, but I'm not sure if it's possible to re-accept, I can at least +1 your answer. – YakovL Apr 20 '16 at 07:50
  • I think you can either edit the question as suggested so the problem is clear or rewrite in different terms to match the accepted answer (somethink like "how to access removable device"). As it's all now, it's rather confusing for whoever googles here in the future looking for help since it incorrectly suggests that PHP has problems with drive letters. – Álvaro González Apr 20 '16 at 08:03
  • @ÁlvaroGonzález fair enough, made it more explicit – YakovL Apr 20 '16 at 08:19

2 Answers2

1

Create a separate directory in your WAMP /www/ directory and make a symbolic link to wherever you need. Some good reading: http://www.tuxera.com/community/ntfs-3g-advanced/junction-points-and-symbolic-links/

bugnumber9
  • 451
  • 1
  • 4
  • 16
  • Again, what happens with this method when the removable drive is removed? – RiggsFolly Apr 19 '16 at 14:05
  • You made me hesitate for a moment, so I went ahead and tested :) Of course, the target won't be available when the stick isn't plugged in. But the symlink itself won't be deleted or something. – bugnumber9 Apr 19 '16 at 14:12
  • So he/she will have to devise a test to prove the stick is in before attempting to write to it – RiggsFolly Apr 19 '16 at 14:19
  • 1
    Well, yes, the stick has to be plugged in. As OP said "I'd like to have some data stored in a removable storage", so with whatever solution the stick has to be in. But what's your point exactly? :) – bugnumber9 Apr 19 '16 at 14:40
  • 1
    PS those who will experiment with this should be aware of how to "safely" remove symlinks (http://superuser.com/questions/167076/how-can-i-delete-a-symbolic-link), good for me I was already aware. – YakovL Apr 20 '16 at 08:11
1

Just a tiny syntax problem:

var_dump("C:\existing\path\copied\from\explorer\new_file.txt");

... prints something like:

string(46) "C:xisting\path\copiedromxplorer
ew_file.txt"

... 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"
Álvaro González
  • 142,137
  • 41
  • 261
  • 360