Demo entry 2842941

Upload Files to Cloud

   

Submitted by Jeremy Alan Muckel on Oct 16, 2015 at 17:18
Language: PHP. Code size: 4.9 kB.

<?php
// include files
print "\nInitializing...\n";
$path = dirname ( dirname ( __FILE__ ) );
require_once $path . '/inc/config.inc';
require_once $path . '/inc/db_connect.inc';

// counters
$found_count = 0;
$copy_count = 0;
$save_count = 0;
$cloud_count = 0;

// make sure the tmp folder exists
if (! file_exists ( "/tmp/uploads" )) {
	mkdir ( "/tmp/uploads", 0777 );
}

// include the Rackspace cloud files
require_once ($path . '/classes/php-opencloud/php-opencloud.php');

// create our Rackspace object
print "Creating RACKSPACE object...\n";
use OpenCloud\Rackspace;
$endpoint = RACKSPACE_ENDPOINT;
$credentials = array (
		'username' => RACKSPACE_USERNAME,
		'apiKey' => RACKSPACE_API_KEY 
);
$_cloud = new Rackspace ( $endpoint, $credentials );
$_cloud->SetDefaults ( 'ObjectStore', 'cloudFiles', 'DFW', 'publicURL' );

// get all current containers
$store = $_cloud->ObjectStore ();
$current = $store->ContainerList ();

// get file data from database (files that aren't already in cloud)
print "Getting file data from database...\n\n";
$files_sql = "SELECT *
				FROM   `files` files
				WHERE file_id IS NOT NULL 
				AND file IS NOT NULL
				AND NOT EXISTS (SELECT 1
								FROM   `files_cloud` cloud
								WHERE  files.file_id = cloud.file_id) LIMIT 5";
$files = mysql_query ( $files_sql, $conn );
if (! $files) {
	exit ( "ERROR! => " . $files_sql . ":\n\n" . mysql_error () . "\n" );
} else {
	if (! mysql_num_rows ( $files )) {
		print "No files found. Exiting...\n";
		exit ();
	}
}

// START MAIN - loop to process each file as a row
while ( $row = mysql_fetch_assoc ( $files ) ) {
	$found_count += 1;
	
	print "Uploading file ID: " . $row ['file_id'] . " (" . $row ['file_name'] . ")...\n";
	
	// get the container name and see if it exists
	$dir = date ( "Y-m-d", strtotime ( $row [create_date] ) );
	
	if (! in_array ( $dir, $containers )) {
		// create the container to store this file in
		try {
			// create it now
			$container = $store->Container ();
			$container->name = $dir;
			$container->Create ();
		} catch ( exception $e ) {
			exit ( "Could not create a new container for " . $dir . ": " . $e->getMessage () . "\n" );
		}
		$containers [] = $dir;
	}
	
	print "Create date TS: " . strtotime ( $row [create_date] ) . "\n";
	print "Container name: " . date ( "Y-m-d", strtotime ( $row [create_date] ) ) . "\n";
	
	// save file locally first
	$row [file_name] = str_replace ( " ", "_", $row [file_name] );
	$row [file_name] = str_replace ( ",", "_", $row [file_name] );
	$file_content = $row [file];
	$filename = '_TESTFILE_' . $row [file_id] . '_' . $row [file_name];
	$temp_path = "/tmp/uploads/" . $filename;
	
	// attempt to save file locally
	$save_file = file_put_contents ( $temp_path, $file_content );
	print "File Size: " . $save_file . " bytes\n";
	
	// ignore files with a blob size of 0 bytes
	if (filesize ( $temp_path ) > 0) {
		if ($save_file == FALSE || $save_file === FALSE) {
			exit ( "FAILED saving local file " . $row [file_id] . "\n" );
		} else {
			print "SUCCESSFULLY saved file ID: " . $row [file_id] . " to " . $temp_path . " -> " . $save_file . " bytes\n";
			$save_count += 1;
		}
		
		// another test
		if (! file_exists ( $temp_path )) {
			exit ( "ERROR! " . $temp_path . " fails file_exists(). Exiting...\n" );
		}
		
		// see if we already have a container
		$container = $store->Container ( $dir );
		
		// attempt to upload file to the cloud
		try {
			$file = $container->DataObject ();
			$file->Create ( array (
					'name' => $filename,
					'content_type' => $row ['file_type'] 
			), $temp_path );
			
			$cloud_count += 1;
		} catch ( exception $e ) {
			exit ( "FAILED! File NOT uploaded to the cloud!\n" );
		}
		
		print "File URL in cloud: " . $file->Url () . "\n";
		
		// copy file data over to cloud table
		$cloud_sql = "INSERT INTO `files_cloud` (`file_id`, `create_date_ts`, `cloud_file_name`, `cloud_file_url`, `cloud_file_hash`)
							VALUES ('" . $row [file_id] . "', '" . strtotime ( $row [create_date] ) . "', '" . $filename . "', '" . $file->Url () . "', '" . $file->hash . "' )";
		
		$cloud = mysql_query ( $cloud_sql, $conn );
		
		if (! $cloud) {
			exit ( "ERROR! => " . $cloud_sql . ":\n\n" . mysql_error () . "\n" );
		} else {
			$copy_count += 1;
		}
	} else {
		print "File size was 0 bytes. Ignoring...\n";
	}
	
	// delete the local file we created, and sleep (just in case)
	@unlink ( $temp_path );
	sleep ( 1 );
	print "\n";
	
} // END MAIN LOOP

print "----------RESULTS----------\n";
print $found_count . " files found not in cloud (including filesize = 0 bytes)\n";
print $save_count . " files saved locally\n";
print $copy_count . " files copied to the new table\n";
print $cloud_count . " files uploaded to the cloud\n";
print "---------------------------\n";

?>

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).