Search code examples
phpdirectoryafp

Connect to remote afp server using PHP


I'm trying to make a php script to connect to an afp server and get a directory listing (with each file size). The server is local in our office, but I'm unable to just make a script on the afp server side. On my machine, I use something like this:

$filesInDir = array();
$filesInMySQL = array();

if (is_dir($uploadDir)) {
    $dh = opendir($uploadDir);
    if ($dh) {
        $file = readdir($dh);

        while ($file != false) {
            $path = $uploadDir . "/" . $file;
            $type = filetype($path);

            if ($type == "file" && $file != ".DS_Store" && $file != "index.php") {
                $filesInDir[] = $file;
            }

            $file = readdir($dh);
        }

        closedir($dh);
    } else {
        echo "Can't open dir " . $uploadDir;
    }
} else {
    echo $uploadDir . " is not a folder";
}

But I can't connect to the afp server. I've looked into fopen it doesn't allow afp, and I don't think it'd allow directory listing:

opendir("afp://ServerName/path/to/dir/");

Warning: opendir() [function.opendir]: Unable to find the wrapper "afp" - did you forget to enable it when you configured PHP? in...
Warning: opendir(afp://ServerName/path/to/dir/) [function.opendir]: failed to open dir: No such file or directory in...`

I'm not looking to see if a file exists, but to get the entire directory listing. Eventually I'll also have to remotely copy files into an output directory.

eg.

mkdir afp://ServerName/output/output001/
cp afp://ServerName/path/to/dir/neededfile.txt afp://ServerName/output/output001/

Solution

  • I'm developing on an Mac Mini, so I realised I could just mount the afp share, and use readdir. I had to mount the drive using the following:

    sudo -u _www mkdir /Volumes/idisk
    sudo -u _www mount_afp -i afp://<IP>/sharename/ /Volumes/idisk/
    

    Further details here