2015-04-28 35 views
1

我正在運行MAMP並使用PHP讀取給定文件夾中所有文件的名稱。我想爲其他文件夾中的某些圖像創建別名,並讓PHP將它們解析爲其實際路徑。我知道如何創建可以這種方式工作的符號鏈接,但是我希望允許不懂技術的網絡所有者使用他們熟悉的Mac OS功能。PHP可以解析Mac別名嗎?

我在同一個文件夾的,我有一個名爲test別名創建一個PHP腳本:

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

此相呼應,「不鏈接」。我曾嘗試在鏈接上使用fread()命令,但似乎PHP掛起。它既不記錄錯誤也不迴應。我已經嘗試在十六進制編輯器中打開別名以查看它包含的內容...但十六進制編輯器打開似乎是大文件(如果別名是文件)或打開目標文件夾(如果別名是到一個文件夾)。

有沒有辦法幫助PHP解析別名中的路徑?是否有我可以撥打的AppleScript功能?

+1

http://php.net/manual/en/function.is-link.php#65706 – Barmar

回答

1

你可能不是指一個實際的符號鏈接。如果您正在處理Finder別名,您可以use the workaround found in the comments for the is_link docs

以下是評論的內容(以避免鏈路只回答後人):

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); 
} 

下面是關於symlinks vs. aliases一些解釋。真的不過,你應該避免使用蘋果的抽象,只是create a symlink

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

太好了!這對我有用。謝謝! –