11

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.

  • What are your options of how to connect to the server? FTP, API or ssh? on the server where you want to store your content – Pontus Abrahamsson Jan 2 '13 at 13:35
  • My only options to connect to server is FTP & SSH – Ruriko Jan 3 '13 at 0:56
  • Use hacklog-remote-attachment plugin https://wordpress.org/plugins/hacklog-remote-attachment/ – saman m Aug 12 '18 at 19:10
  • I understand the OP indicated that API access is not an option, however on most accessible sites the new WordPress API is always available. Since this is an old thread, thought I'd mention that for other visitors. For instance, if you can access the website, you can typically access the API. Examples regarding media: developer.wordpress.org/rest-api/reference/media – ryanm Oct 2 '18 at 16:00
14
+50

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.

  • that worked great uploading is ok but got an error Warning: ftp_put() [function.ftp-put]: Could not create file. in /home/xxx..../plugin_file.php on line 99 – Fatih Toprak Jun 23 '13 at 10:12
  • @Pontus Abrahamsson, Thanks for this great solution. However how can I disable that it start sending every media file on the server to the other server? I just want a newly uploaded file being transfered via ftp. – user3605780 Aug 17 '16 at 8:58
0

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.

  • Yes it similar to CDN except I don't have enough space on Server A cause it only has 5GB while Server B has 1TB of space. – Ruriko Jan 4 '13 at 0:34
0

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.

-1

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.

  • I can't use SSHFS or FTPFS because my host doesn't support FUSE – Ruriko Jan 3 '13 at 5:34
  • Then switch hosts. The biggest issue with implementing your request is not the upload to the other server part (just copy code from plugins which upload to CDN), but how to fetch it from there to serve it. Without file system level access to server B apache will not be able to serve the files, so you will have to make server B accessible to the web, but then you can just move your WordPress there. You really didn't give enough details on your situation. Please update the question with all relevant detail if you want to have a chance at getting an answer. – Mark Kaplun Jan 3 '13 at 7:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.