James Newton
James Newton

Reputation: 7104

Can PHP resolve a Mac alias?

I am running MAMP and using PHP to read the names of all the files in a given folder. I would like to create an alias for certain images in another folder, and have PHP resolve these to their actual path. I know how to create symlinks that will work this way, but I would like to allow non-tech savvy web owners to use the Mac OS features that they are familiar with.

I have created a PHP script in the same folder as a alias that I have named test:

<?php
if (is_link ("test")) {
 echo "is link";
} else {
 echo "is not link";
}
?>

This echoes "is not link". I have tried using the fread() command on the link, but PHP seems to hang. It neither logs an error nor responds. I have tried opening the alias in a hex editor to see what it contains... but the hex editor opens what seems to be a huge file (if the alias is to a file), or opens the target folder (if the alias is to a folder).

Is there a way to help PHP to resolve the path in the alias? Is there an AppleScript function that I can call?

Upvotes: 2

Views: 667

Answers (1)

sjagr
sjagr

Reputation: 16502

Chances are you're not referring to an actual symbolic link. If you're dealing with Finder Aliases, you can use the workaround found in the comments for the is_link docs.

Here are the contents of the comment (to avoid a link-only answer for posterity):

if( getFinderAlias( $someFile , $target ) ) {
echo $target;
}
else {
echo "File is not an alias";
}

function getFinderAlias( $filename , &$target ) {
$getAliasTarget = <<< HEREDOC
    -- BEGIN APPLESCRIPT --
    set checkFileStr to "{$filename}"
    set checkFile to checkFileStr as POSIX file
    try
        tell application "Finder"
            if original item of file checkFile exists then
                set targetFile to (original item of file checkFile) as alias
                set posTargetFile to POSIX path of targetFile as text
                get posTargetFile
            end if
        end tell
    end try
    -- END APPLESCRIPT --
HEREDOC;
$runText = "osascript << EOS\n{$getAliasTarget}\nEOS\n";
$target = trim( shell_exec( $runText ) );
return ( $target == "" ? false : true );
}

Here's some explanation about symlinks vs. aliases. Really though, you should avoid using Apple's abstractions and just create a symlink:

ln -s /path/to/file /path/to/symlink

Upvotes: 1

Related Questions