:::
顯示具有 系統/WordPress 標籤的文章。 顯示所有文章

解決WordPress User-Avatar外掛無法連結的問題 / Solution for WordPress’s Plugin User-Avatar Error

解決WordPress User-Avatar外掛無法連結的問題 / Solution for WordPress’s Plugin User-Avatar Error

image

最近我將WordPress掛載NFS之後,WordPress的外掛User Avatar就無法順利運作。原因是因為timthumb縮圖程式無法寫入暫存檔案,修改程式碼之就能解決問題。

Recently, I try to integrate NFS with WordPress’s upload directory, and then the plugin User-Avatar got broken. This problem is due to thumb program “user-avatar-pic.php” could not write lock files. I modified this program to solve it.


問題敘述:縮圖程式錯誤訊息 / Problem Description: TimThumb Error

WordPress與外掛User-Avatar / WordPress’s Pluging User-Avatar

screenshot-1

我目前使用的WordPress與外掛User-Avatar版本資訊如下:

User Avatar是允許讓使用者自行上傳大頭照的外掛,簡單好用。

uploads資料夾掛載到NFS / Mount “uploads” Directory to NFS

最近我將WordPress存放附件檔案的資料夾uploads改掛載到NFS底下。具體來說是資料夾路徑是:

[WordPress]/wp-content/uploads

image

圖片無法讀取 / Could Not Load Image

掛載之後,附件大多都能正常下載,可是卻發現圖片無法讀取的問題。

2014-03-12_000853

上圖紅框的部份可以看到,應該出現作者大頭像的地方卻沒有顯示了,非常奇怪。

image

直接打開網址之後,就發生了錯誤訊息:

A TimThumb error has occured

The following error(s) occured:
  • Could not get a lock for writing.

Query String : src=http://********/wp-content/uploads/avatars/46/1371804156-bpfull.jpg&w=75&id=46&random=1392976007
TimThumb version : 2.8.10

問題剖析 / Problem Analysis

簡單來說,是程式碼中有些段落發生了無法順利寫入的問題。發生錯誤的程式碼是外掛User-Avatar的user-avatar-pic.php,檔案路徑如下:

[WordPress]/wp-content/plugins/user-avatar/user-avatar-pic.php

根據錯誤訊息「Could not get a lock for writing.」,我們可以找到發生錯誤的程式碼,以及回溯到前面開啟檔案的相關程式:

$lockFile = $this->cachefile . '.lock';
$fh = fopen($lockFile, 'w');
if(! $fh){
    return $this->error("Could not open the lockfile for writing an image.");
}
if(flock($fh, LOCK_EX)){
    @unlink($this->cachefile); //rename generally overwrites, but doing this in case of platform specific quirks. File might not exist yet.
    rename($tempfile4, $this->cachefile);
    flock($fh, LOCK_UN);
    fclose($fh);
    @unlink($lockFile);
} else {
    fclose($fh);
    @unlink($lockFile);
    @unlink($tempfile4);
    return $this->error("Could not get a lock for writing.");
}

注意紅字的第一行。這一行是指定寫入檔案的路徑,顯然地目前這個路徑設定是不可寫入的。要確保暫存檔能夠正常寫入,一般來說都是寫在暫存目錄底下。PHP取得暫存目錄並在之中產生暫存檔案的作法是用tempnam(sys_get_temp_dir(), 'FOO'),因此程式碼應該替換掉第一行變成:

//$lockFile = $this->cachefile . '.lock';
$lockFile = tempnam(sys_get_temp_dir(), 'timthumb') . '.lock';

這樣子就能夠讓網頁正常顯示了。

2014-03-12_003035


解決方案:替換user-avatar-pic.php / Solution: Replace file “user-avatar-pic.php”

以下講述簡單的作法:

  1. 下載user-avatar-pic.php (我已經上傳到GitHub)
  2. 替換掉原本檔案,路徑是「[WordPress]/wp-content/plugins/user-avatar/user-avatar-pic.php

這樣就完成了。

(more...)