PHPBuilder - Samba filesystem mounting



RSS Twitter
Snippets File Management

Samba filesystem mounting

by: Willem Vooys
|
January 29, 2003

Version: 1.14

Type: Function

Category: File Management

License: GNU General Public License

Description: These functions let you easily mount a (windows) network share on your linux box.



function smb_connect($share, $localdir, $username="", $password="")
{
  //this function is dependent of 'is_mounted()', also found here
  //remember that mount can only be executed by the super-user, 
  //unless the mount command is already in etc/fstab with the 
  //'user' option enabled:see the manual entry for mount and/or fstab
  
  $umount="umount ".$localdir;
  $smbmount="smbmount ".$share." ". $localdir;

  if($username != "")
  {
   $smbmount.=" -o username=".$username;
   if($password != "")
     $smbmount.=",password=".$password;
  }
  
  if(is_mounted($share))
    RETURN TRUE;
  else
  {
    exec($umount); //just to be sure
    exec($smbmount); //try to mount it
    
    if(is_mounted($share))
      RETURN TRUE;
    else
      RETURN FALSE;
  }
}

function smb_disconnect($dir)
{
  exec("umount ".$dir);
}

function is_mounted($device)
{
  exec("mount", $mounted);
  for($i=0;$i<count($mounted);$i++)
  {
    $this_device=explode(" ", $mounted[$i]);
    if($this_device[0] == $device)
      $found=TRUE;
  }
  if($found)
    RETURN TRUE;
  else
    RETURN FALSE;
}

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Willem Vooys

Comment:



Comment:

(Maximum characters: 1200). You have characters left.