Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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

5 Answers 5

up vote 4 down vote accepted
+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.

share|improve this answer
    
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

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.

share|improve this answer
    
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

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.

share|improve this answer
    
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

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.

share|improve this answer

please have a look at your functions.php on line 1433 you will find following function

function wp_upload_dir( $time = null ) {
global $switched;
$siteurl = get_option( 'siteurl' );
$upload_path = get_option( 'upload_path' );
$upload_path = trim($upload_path);
$main_override = is_multisite() && defined( 'MULTISITE' ) && is_main_site();
if ( empty($upload_path) ) {
    $dir = WP_CONTENT_DIR . '/uploads';
} else {
    $dir = $upload_path;
    if ( 'wp-content/uploads' == $upload_path ) {
        $dir = WP_CONTENT_DIR . '/uploads';
    } elseif ( 0 !== strpos($dir, ABSPATH) ) {
        // $dir is absolute, $upload_path is (maybe) relative to ABSPATH
        $dir = path_join( ABSPATH, $dir );
    }
}

if ( !$url = get_option( 'upload_url_path' ) ) {
    if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )
        $url = WP_CONTENT_URL . '/uploads';
    else
        $url = trailingslashit( $siteurl ) . $upload_path;
}

if ( defined('UPLOADS') && !$main_override && ( !isset( $switched ) || $switched === false ) ) {
    $dir = ABSPATH . UPLOADS;
    $url = trailingslashit( $siteurl ) . UPLOADS;
}

if ( is_multisite() && !$main_override && ( !isset( $switched ) || $switched === false ) ) {
    if ( defined( 'BLOGUPLOADDIR' ) )
        $dir = untrailingslashit(BLOGUPLOADDIR);
    $url = str_replace( UPLOADS, 'files', $url );
}

$bdir = $dir;
$burl = $url;

$subdir = '';
if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
    // Generate the yearly and monthly dirs
    if ( !$time )
        $time = current_time( 'mysql' );
    $y = substr( $time, 0, 4 );
    $m = substr( $time, 5, 2 );
    $subdir = "/$y/$m";
}

$dir .= $subdir;
$url .= $subdir;

$uploads = apply_filters( 'upload_dir', array( 'path' => $dir, 'url' => $url, 'subdir' => $subdir, 'basedir' => $bdir, 'baseurl' => $burl, 'error' => false ) );

// Make sure we have an uploads dir
if ( ! wp_mkdir_p( $uploads['path'] ) ) {
    if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )
        $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
    else
        $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];

    $message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $error_path );
    return array( 'error' => $message );
}

return $uploads;}

make the appropriate changes in this function to make the file upload to remote server also you will need to take care of the upload host permissions and fetching of those file for display purpose in wordpress

PS:- I know that editing core files wont be the best option but rather than relying on some third party plugin it is always better to have it coded in the system so that you know what you are doing. Also it benefits because you know what the code does and you can add all the functionality you need. But yes i won't recommend this to a newbee as they might mess up the whole thing. In my opinion don't do this as it will increase the site load time because it will be querying remote host for to display the image. It will have to wait for the server to respond for the query adding more time to the site load.

share|improve this answer
7  
You never want to make modifications to WordPress core files. There are always other ways to accomplish a task. –  Matthew Boynes Jan 2 '13 at 18:36
    
I'm not a programmer so I don't which part needs to be changed –  Ruriko Jan 3 '13 at 1:05
1  
You could rewrite this function or use filters and hooks to change the default behaviour. You can't do it without using a custom plugin. –  Ciprian Jan 3 '13 at 13:34
    
This is the worst idea since WordPress introduced add_action() and add_filter() –  Ralf912 Jan 4 '13 at 0:43
    
In my opinion he does not want any condition on whether to transfer it to remote or to transfer it to local.so what is the harm if someone hard codes the new value. any ways he cannot directly store the value to remote server without having proper access to that server so it is useless to do any change or adding any hook/filter –  Ketan Patkar Jan 5 '13 at 6:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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