Search code examples
phposx-mavericksshared-directory

PHP access to shared folder on Mac


I have to build the function to scan all folders, subfolders, and files on directory. I am able to do that now if its my own mac directories.

$parent_link = '/Users/myusername/Downloads';
$directory = dirToArray($parent_link);
var_dum($directory);

function dirToArray($dir) { 

   $result = array(); 

   $cdir = scandir($dir); 
   foreach ($cdir as $key => $value) 
   { 
      if (!in_array($value,array(".",".."))) 
      { 
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
         { 
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
            //var_dump($result[$value]);
         } 
         else 
         { 
             $result[] = $value; 
         } 
      } 
   } 

   return $result; 
} 

However, How php access shared folder on Mac?

current situation :

I can access the shared folder on Mac using finder (with permission).

The folder is currently shared by mac server (LAN).

php is installed on my computer.


Solution

  • I changed the method of how I access the shared server. Instead of forcing it to access the shared folder by changing the path (which I'm still not able to figure it out), I am using php ftp. The working code is as follow :

    $hostname = '192.168.X.X';
    $username = 'username';
    $password = 'password';
    $conn_id = ftp_connect($hostname);
    $login = ftp_login($conn_id, $username, $password);
    $parent_link = 'Document';
    if (!$conn_id) {
        echo 'Wrong server!';
        exit;
    } else if (!$login) {
        echo 'Wrong username/password!';
        exit;
    } else {
        $directory = dirToArray($conn_id, $parent_link);
        var_dump($directory);
    }
    
    function dirToArray($ftpConnection, $dir) { 
    
        $result = array(); 
    
        $cdir = ftp_nlist($ftpConnection, $dir); 
        foreach ($cdir as $key => $value) 
        { 
            $subs_value = substr($value, strlen($dir) + 1);
            // assuming its a folder if there's no dot in the name 
            if (strpos($value, '.') === false) {
                $result[$subs_value] = dirToArray($ftpConnection, $value);
            }
            else
            {
                $result[] = $subs_value;
            }
        } 
        return $result; 
    }