Horje
Mirrors new uploads silently to several backup domains - Simple! - Wordpress Solution
I have 3 different websites on 3 different shared-servers/vps. I want... website#1 to act as the mirror/backup for ALL uploaded files. website#2 should only backup the images (png,gif, jpeg). website#3 should only backup the .psp, .psd, .mp3, .flv, and .mp4 files. The backup websites should be able to create their own folders so that it matches the same folders as the main website. "wp-content/uploads/2010/01" Please take a look at the related post below so you can see the code that I use on my website. RELATED POST: [[LINK href="http://wpquestions.com/question/show/id/752"]]http://wpquestions.com/question/show/id/752[[/LINK]] THIS IS THE CODE THAT WE HAVE ALREADY (working). It mirrors all uploads silently to our backup website#1. Now I need to improve this plugin so I can backup only CERTAIN files to website#2 and website#3. plugin: uploadmirror/uploadmirror.php
<?php 

/*

Plugin Name: Automatic Upload Mirror

Plugin URI: 

Description: 

Author: 

Version: 1.0

Author URI: 

*/





add_filter('wp_handle_upload', 'uploadmirror_main');

add_filter('wp_create_thumbnail', 'uploadmirror_main');

add_filter('image_make_intermediate_size', 'uploadmirror_main');



function uploadmirror_main($file) {



	$uploads = wp_upload_dir();

	$uploads['file'] = (is_string($file) ? $file : $file['url']);

	$uploads['dir'] = (is_string($file) ? str_replace($uploads['basedir'], '', $uploads['file']) : str_replace($uploads['baseurl'], '', $uploads['file']));



	// Fix url for thumbnails

	$uploads['file'] = str_replace($uploads['path'], $uploads['url'], $uploads['file']);



	foreach (array('uploadmirror_domain', 'uploadmirror_domain1', 'uploadmirror_domain2') as $option_name)

	{

		if (get_option($option_name) !== false) {



			$ch = curl_init();

			curl_setopt($ch, CURLOPT_POST, true);

			curl_setopt($ch, CURLOPT_POSTFIELDS, 'file=' . $uploads['file'] . '&dir=' . $uploads['dir']);

			curl_setopt($ch, CURLOPT_URL, get_option($option_name));

			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

			$postResult = curl_exec($ch);



			curl_close($ch);

		}

	}



	return $file;



}





//Dashboard Menus



add_action('admin_menu', array('uploadmirror_admin_content', 'menu'));





class uploadmirror_admin_content {



	function menu() {

		add_options_page('Automatic Upload Mirror', 'Automatic Upload Mirror', 8, __FILE__, array('uploadmirror_admin_content', 'modify'));

	}

		

	function modify() {

		if (!empty($_POST['do'])) {

			foreach (array('uploadmirror_domain', 'uploadmirror_domain1', 'uploadmirror_domain2') as $option_name)

			{

				$newvalue = $_POST[$option_name];

				if (get_option($option_name) != $newvalue) {

					update_option($option_name, $newvalue);

				} else {

					$deprecated=' ';

					$autoload='no';

					add_option($option_name, $newvalue, $deprecated, $autoload);

				}

			}



			if (!empty($error))

			{

				echo '<div class="error fade">' . nl2br($error) . '</div>';

			}

			else

			{

				echo '<div class="updated fade">Settings saved.</div>';

			}

		}



		?>



	<div class="wrap">



		<h2>Automatic Upload Mirrors</h2>



			<form action="" method="post" enctype="multipart/form-data">



				<TABLE class="w3-table w3-striped w3-bordered w3-border w3-white" class="widefat">



					<tbody>



						<tr>



							<td>Path to filedump script #1</td>



							<td><input type="text" value="<?php echo get_option('uploadmirror_domain', ''); ?>" name="uploadmirror_domain" style="width: 100%;" /></td>



						</tr>



						<tr>



							<td>Path to filedump script #2</td>



							<td><input type="text" value="<?php echo get_option('uploadmirror_domain1', ''); ?>" name="uploadmirror_domain1" style="width: 100%;" /></td>



						</tr>



						<tr>



							<td>Path to filedump script #3</td>



							<td><input type="text" value="<?php echo get_option('uploadmirror_domain2', ''); ?>" name="uploadmirror_domain2" style="width: 100%;" /></td>



						</tr>



					</tbody>



				</table>



			<input class="button-primary" name="do" value="Submit" class="button" type="submit" />



			&nbsp;&nbsp;



			<input name="cancel" value="Cancel" class="button" onclick="javascript:history.go(-1)" type="button" />



		</form>



	</div>



	<?php



	}

}

?>
And here's the code for the accompanying 'helper' file that downloads the uploads on the remote server: website#1 /wp-content/uploads/filedump.php
<?php

function vWritePageToFile($readurl)

{

	if (!$readurl)

	{

		print "No file specified.";

		return false;

	}

	

	$dir = $_POST['dir'];

	$parts = explode('/', $dir);

	$file = array_pop($parts);

	$dir = dirname(__FILE__);



	foreach ($parts as $part)

	{

		if (!empty($part))

		{

			if (!is_dir($dir .= "/$part")) 

			{

				mkdir($dir);

			}

		}

	}



	$ch = curl_init($readurl);

	curl_setopt($ch, CURLOPT_HEADER, 0);

	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

	$rawdata = curl_exec($ch);

	curl_close($ch);



	if (file_exists("$dir/$file"))

	{

		unlink("$dir/$file");

	}



	$fp = fopen("$dir/$file", 'x');

	fwrite($fp, $rawdata);

	fclose($fp);

}



if (isset($_POST['file']))

{

	vWritePageToFile($_POST['file']);

}

else

{

		die("No file specified.");

}

?>


Solution - 1

Didnt Jonah Schulte solve your problem before?





Wordpress

Related
Changing the blog title to an image/logo in Oulipo Theme - Wordpress Solution Changing the blog title to an image/logo in Oulipo Theme - Wordpress Solution
working example of dynamic archives - Wordpress Solution working example of dynamic archives - Wordpress Solution
Placing an array within a a loop - Wordpress Solution Placing an array within a a loop - Wordpress Solution
wp_get_attachment() resize mod needed - Wordpress Solution wp_get_attachment() resize mod needed - Wordpress Solution
live ajax sorting of custom taxonomies for a custom post-type - Wordpress Solution live ajax sorting of custom taxonomies for a custom post-type - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9