問題描述
我正在使用 Wordpress 媒體上傳器上傳影像,我想知道是否有辦法將其上傳到我的遠端伺服器而不是我的本地伺服器?示例:我的 wordpress 部落格託管在伺服器 A 上,我想要使用媒體上傳器將影像上傳到伺服器 B 而不是伺服器 A.
兩臺伺服器都使用 nginx 作為網路伺服器執行 Ubuntu 10.04 LTS,並且都支援 ftp& SFTP 。
最佳解決方案
我剛剛構建了一個這樣做的外掛。它不是完美的,但它做它的工作。
你可以在我的 Github 找到它:https://github.com/pontusab/wp-ftp-media-library
所以你需要改變第 28 行檔案中的一些內容:
/**
* Change this to match your server
* You only need to change the those with (*)
* If marked with (-) its optional
*/
$settings = array(
'host' => 'ip or hostname', // * the ftp-server hostname
'user' => 'username', // * ftp-user
'pass' => 'password', // * ftp-password
'cdn' => 'cdn.example.com', // * This have to be a pointed domain or subdomain to the root of the uploads
'path' => '/', // - ftp-path, default is root (/). Change here and add the dir on the ftp-server,
'base' => $upload_dir['basedir'] // Basedir on local
);
這個外掛是什麼,它將上傳結構從/year /month 更改為僅在我們的伺服器 A 的情況下在本地機器上上傳。然後它使用 php 透過以下方式連線到 ftp:ftp_connect 。當 wp_generate_attachment_metadata 執行時,該功能將觸發。然後執行一個 check in the upload folder to see if there areu any images,如果是這樣,它將全部上傳到 ftp-server 透過 ftp_put 。上傳完成後,檔案將使用 unlink 函式從本地機器中刪除。
然後外掛將影像的 URL 更改為指向 ftp-server 的”public” ip 或主機名。我建議使用類似 static.mydomain.com 或 cdn.mydomai.com 。他們需要指向 ftp-server(伺服器 B),這使您能夠從 ftp-server 載入影像。
正如其他成員所說,你應該使用一個 ftp-server,它更好的一個真正的 cdn,由保險絲或像亞馬遜 S3 這樣的東西。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。