Horje
Mirrors new uploads silently to a backup domain - Simple! - Wordpress Solution
This plugin WAS working before.. I played around with some of the code too much and now it doesnt work. )= I simply need the SAME functionality that "Amazon S3 Plugin" offers. Anything that I upload, is backed up onto my S3 account. Now I need to expand on that idea and ALSO have a mirror/backup plugin to a custom backup domain. ( http://backup-domain.com/backups/... ) (plugin not working) This is what I have as a plugin right now:

<?php 
add_filter('wp_handle_upload', 'uploadmirror_main');
add_filter('wp_create_thumbnail', 'uploadmirror_main');
add_filter('image_make_intermediate_size', 'uploadmirror_main');
add_filter('wp_handle_upload', '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']));

	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 <strong>saved</strong>.</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
	}


}
?>
----------- On my OTHER domain.. I have this in a folder (chmodd "777"):

<?php
function vWritePageToFile($readurl)
{
	$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);
}

vWritePageToFile($_POST['file']);
?>


Solution - 1

I've got it working beautifully now on my test environment... Here's the revised/fixed code for the plugin:


<?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:

<?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.");
}
?>
Put the helper file at the root of where the backed up files should be stored. For example, if you're trying to mirror the files from one Wordpress installation to another, the place to put the helper file is /wp-content/uploads/ because that is the base directory for the uploaded content. This may be obvious, but you'll have to activate the "Automatic Upload Mirror" plugin in the wordpress admin and also configure the location of your remote web server's helper file in the "Automatic Upload Mirror" area under "Settings" in the admin. If you put the helper file (let's say it's called backup-mirror.php) in /wp-content/uploads/ on your wordpress install and your domain is www.mydomain.com, enter this in the "Path to filedump script #1" field in the plugin settings: http://www.mydomain.com/wp-content/uploads/backup-mirror.php If you prefer to put the helper file in another location you can modify this line in the helper script above: $dir = dirname(__FILE__); If you want to put the script in the root folder of the Wordpress install for example, change the above line to: $dir = dirname(__FILE__).'/wp-content/uploads'; And then update the plugin settings to: http://www.mydomain.com/backup-mirror.php Let me know if you have any problems with this code or any questions! Thanks, Jonah


Solution - 2

Can you post a working copy of the plugin, before you modified it?


Solution - 3

Why in gods name would you use curl to move a file??





Wordpress

Related
Wordpress Paypal Javascript - Wordpress Solution Wordpress Paypal Javascript - Wordpress Solution
Show horizontal login form, change colour & show button as image - Wordpress Solution Show horizontal login form, change colour & show button as image - Wordpress Solution
How do I optimise my Wordrepss database? - Wordpress Solution How do I optimise my Wordrepss database? - Wordpress Solution
Flutter Help - Wordpress Solution Flutter Help - Wordpress Solution
Add logo to header & Change menu  - Wordpress Solution Add logo to header & Change menu - Wordpress Solution

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