問題描述

我正在使用 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。