Upload images to remote server

The question:

I am using WordPress media uploader to upload images and I was wondering if there is a way to make it upload to my remote server instead of my local server? Example: My wordpress blog is hosted on Server A and I want to want to use the media uploader to upload images to Server B instead of Server A.

Both servers run Ubuntu 10.04 LTS with nginx as the webserver and both support ftp & sftp.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I have just built a plugin that does this. It is not perfect but it do its job.

You can find it in my Github: https://github.com/pontusab/wp-ftp-media-library

So you need to change some things within the file on the row 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 

What this plugin does is, it changes the upload structur from /year/month to only upload on the local machine in our case Server A. Then it uses php to connect to the ftp via: ftp_connect. The function fires when wp_generate_attachment_metadata runs. It then run a check in the upload folder to see if there areu any images, if so it will upload them all to the ftp-server via ftp_put. When the upload is completed the files will be removed from the local machine using the function unlink.

Then the plugin changes the url of the images to the “public” ip or hostname pointed to the ftp-server. I suggest using something like static.mydomain.com or cdn.mydomai.com. They need to be pointed to the ftp-server (Server B) this enables you to load the images from the ftp-server.

As the other members say you shouldent use an ftp-server for this, its better with an real cdn, Mounted by fuse or something like Amazon S3.

Method 2

It almost sounds like you just want to use a CDN. The way most CDNs work is you load your images locally, and they get mirrored at the CDN location. Then you set up a domain that points to the CDN content and rewrite your image urls to use that domain instead of your normal url.

There are several good plugins and services, although I only have experience with W3TC and Cloudfront.

Method 3

You have to understand how WP uploads a file. A POST request send the file to the server where it will be stored in a temporary folder. After that, WP will test the uploaded file, copy it into the content directory and delete the temporary file.

This is a very complex process. For images, you can hook into ‘media_upload_file’, then check if it is an image. If it is an image, copy it with WP_Filesystem to the external server (use method ftpext and provide your ftp credentials within the options), else let WP handle the upload with wp_handle_upload().
In case of copy the file to an external server, you have to provide the url and so on to WP, so WP can setup the data (e.g. for attachment posts).

This job isn’t done with a hand full of code. If you are not a developer, better hire one. Or search for a plugin which will done this for you.

Method 4

You can use SSHFS or FTPFS to mount a directory on the remote server for your uploads directory. This way you will not need t change anything in WordPress itself.

The drawback is that every access to the uploaded file will be slower depending on where server B resides.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment