php to access shared smb folder (user/password protected)

11,886

Solution 1

You can specify the username and password in the path: //user:password@server/folder1/file1.txt

Solution 2

I had the exact same problem and Sjoerd's answer didn't work for me on Windows.

I wrote a mounting function in PHP which basically execs:

net use "\\smbserver\share" /user:"myuser" "mypassword" /persistent:no

Then you can normally access paths like \\smbserver\share/path/file.

This works for all functions specified in the options table for file wrappers:

  • Allows Reading
  • Allows Writing
  • Allows Appending
  • Allows Simultaneous Reading and Writing
  • Supports stat()
  • Supports unlink()
  • Supports rename()
  • Supports mkdir()
  • Supports rmdir()

This works perfectly stable on remote shares but didn't work for me when I used localhost with apache as SYSTEM.

Don't forget to unmount afterwards by execing:

net use "\\smbserver\share" /delete /yes

I hope this helps.

Share:
11,886
Jiří Doubravský
Author by

Jiří Doubravský

Updated on August 21, 2022

Comments

  • Jiří Doubravský
    Jiří Doubravský over 1 year
    1. WAMP server
    2. PHP fopen function needs to open file in shared folder //server/folder1/file1.txt
    3. php has SYSTEM user permisions, but shared folder is visible only for userX/password

    how can i open this remote file inside php script?