Search code examples
phplinuxwindowsfile-permissionssamba

Read Windows Samba Share Directory with PHP from LAMP Server


I have a Samba share from Windows network mounted to a directory on my Linux based Webserver. I have mounted the directory as follows:

mount -t cifs -o username=admin,password='passsword',domain=mydomain.local,file_mode=0644,dir_mode=0777,uid=client_user,gid=client_user '//192.168.0.x/d$' /home/client_user/mnt

The mount works and I can browse through the files and directories in the OS. However, I wish to be able to access this through a PHP script ran from the browser. However, any file operations on the share result in a permission denied error. I have experimented a little and replaced the uid and gid parameter values with apache, but still no luck.

Any suggestions are much appreciated

Edit

In further tests I have created a file with the following code:

if(is_readable('/path/to/mnt')) {
  echo 'Readable';
}
else {
  echo 'Not';
}

Running this from the command line on the server results in Readable being printed. I have ran this as root and as a user on the server, but it will not work from the browser.


Solution

  • So after some trial and error I worked out that SELinux was not permitting httpd access to the folders.

    Running this command allows httpd to access cifs:

    setsebool -P httpd_use_cifs on
    

    However, further investigation revealed that I could set the httpd context on just the mounted folder. So I unmounted the drive and amended my mount command to include:

    context="system_u:object_r:httpd_sys_rw_content_t:s0",
    

    The full command:

    mount -t cifs -o context="system_u:object_r:httpd_sys_rw_content_t:s0",username=admin,password='passsword',domain=mydomain.local,file_mode=0644,dir_mode=0777,uid=client_user,gid=client_user '//192.168.0.x/d$' /home/client_user/mnt