<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Delaware Web Designers - Inclind, Inc Internet Professionals &#187; using Services module in drupal</title>
	<atom:link href="http://www.delawarewebdesigner.com/tag/using-services-module-in-drupal/feed" rel="self" type="application/rss+xml" />
	<link>http://www.delawarewebdesigner.com</link>
	<description>Inclind, Inc - Delaware Web Designers - Professional Delaware Web Design Since 1999</description>
	<lastBuildDate>Wed, 12 May 2010 16:21:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sync Files in Drupal Using Services and xmlrpc()</title>
		<link>http://www.delawarewebdesigner.com/how-tos/sync-files-in-drupal-using-services-and-xmlrpc.htm</link>
		<comments>http://www.delawarewebdesigner.com/how-tos/sync-files-in-drupal-using-services-and-xmlrpc.htm#comments</comments>
		<pubDate>Tue, 05 Jan 2010 14:24:08 +0000</pubDate>
		<dc:creator>Kevin Quillen</dc:creator>
				<category><![CDATA[Content Management]]></category>
		<category><![CDATA[Custom Website Design]]></category>
		<category><![CDATA[Drupal Development]]></category>
		<category><![CDATA[How To's]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web Applications]]></category>
		<category><![CDATA[drupal modules]]></category>
		<category><![CDATA[content distributing in drupal]]></category>
		<category><![CDATA[sync files with drupal xmlrpc]]></category>
		<category><![CDATA[using drupal xmlrpc]]></category>
		<category><![CDATA[using Services module in drupal]]></category>

		<guid isPermaLink="false">http://www.delawarewebdesigner.com/?p=486</guid>
		<description><![CDATA[In a previous entry we explored content syncing/distributing using the Services module and XMLRPC in Drupal. We learned how to create a method, its callback, how to invoke it through xmlrpc from another server, and how to create or update a node based on that data.
Lets take a look at how you bring in files [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.delawarewebdesigner.com/tech-stuff/sync-drupal-content-using-services-and-xmlrpc.htm">previous entry</a> we explored content syncing/distributing using the <a href="http://www.drupal.org/project/services" target="_blank">Services</a> module and XMLRPC in Drupal. We learned how to create a method, its callback, how to invoke it through xmlrpc from another server, and how to create or update a node based on that data.</p>
<p>Lets take a look at how you bring in files as part of that node as well. From the previous example, before calling node_save($node), I call another function to process files. Name it however you wish. My module is called homes_sync, so my hooks and functions begin with that as per Drupal standards.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">homes_sync_get_files<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nid</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sessid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// save the node object</span>
node_save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>homes_sync_get_files() is passed the $node object and will return it after adding (or removing) data. To have a reliable file sync, we need to only update files if they are newer, and remove them if they no longer exist.</p>
<p>In this specific example, the project I am working on has two CCK file fields. One is for a floorplan (a PDF) and the other is an image field. In a node, there is only 1 floorplan, but can be up to 2 dozen images. If you had a more generic node that just has a CCK filefield of &#8216;client_files&#8217; for example, your code would be slightly different.</p>
<p>Before we add any files to the node, the first thing we should do is compare the list of files from the remote site to the local site. We start that off by getting $files via xmlrpc, and loading the node object if $node->nid is present. If any are found on the local site, and not the remote site, we can safely remove them from the node object. The two arrays are iterated over and remove files if necessary.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> homes_sync_get_files<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nid</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sessid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> xmlrpc<span style="color: #009900;">&#40;</span>REMOTE_SERVICES_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'file.getHomeFiles'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sessid</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// first, compare list of files from parent server with local files</span>
	<span style="color: #666666; font-style: italic;">// if our local files has something the parent server doesnt have</span>
	<span style="color: #666666; font-style: italic;">// it was probably deleted. so we need to unset it from the node object</span>
	<span style="color: #666666; font-style: italic;">// when it gets passed back through node_save, cck should delete it proper</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$current_files</span> <span style="color: #339933;">=</span> node_load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_files</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// we have files locally, but none on the parent server. 	</span>
			<span style="color: #666666; font-style: italic;">// remove all</span>
			<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_floorplan</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'homes_sync'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Remote copy of @node no longer has files. Removing all local files for @node.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@node'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> WATCHDOG_NOTICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_files</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// build comparison arrays</span>
&nbsp;
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$remote_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$remote_files</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$remote_file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_files</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_floorplan</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$local_floorplan_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$local_files</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$local_floorplan_file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_files</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_image</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$local_image_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$local_files</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$local_image_file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// now lets see whats missing</span>
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$local_files</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$remote_files</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
				<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$local_files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$local_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$local_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$remote_files</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #666666; font-style: italic;">// parent site does not have this file in the node anymore</span>
						<span style="color: #000088;">$files_to_delete</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$local_file</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files_to_delete</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$files_to_delete</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files_to_delete</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$file_to_delete</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #666666; font-style: italic;">// CCK field arrays exist in node objects even if they have no data</span>
						<span style="color: #666666; font-style: italic;">// we can assume if the first record is NULL, there is no data</span>
&nbsp;
						<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_floorplan</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
							<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_floorplan</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file_key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$floorplan</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
								<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_to_delete</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$floorplan</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
									watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'homes_sync'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@file file does not exist on parent server anymore for @node. Removing.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@file'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$floorplan</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@node'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> WATCHDOG_NOTICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
									<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_floorplan</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$file_key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
								<span style="color: #009900;">&#125;</span>
							<span style="color: #009900;">&#125;</span>
						<span style="color: #009900;">&#125;</span>
&nbsp;
						<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_image</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
							<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_image</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$image_key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
								<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_to_delete</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$image</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
									watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'homes_sync'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@image image does not exist on parent server anymore for @node. Removing.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@image'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$image</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@node'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> WATCHDOG_NOTICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
									<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_image</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$image_key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
								<span style="color: #009900;">&#125;</span>
							<span style="color: #009900;">&#125;</span>
						<span style="color: #009900;">&#125;</span>
						<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$floorplan</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$remote_files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$local_files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now that we are through with our deletion process, we can begin processing the remote files. For each file id ($fid) we call xmlrpc and get all the data and the file itself and bring it down to our local site. Based on the file extension, we begin building a query so we can look to see if this file exists locally, and what its last timestamp was. We do this so we know if a file should be replaced or not. For example, if you have MyHome.PDF on your local site, and a new version is uploaded on the remote site, simply comparing file names is not good enough. With a filename and a timestamp comparison, you are certain that it should be processed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$fid</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$remote_file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> xmlrpc<span style="color: #009900;">&#40;</span>REMOTE_SERVICES_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'file.get'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sessid</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// set filepath to save to</span>
				<span style="color: #666666; font-style: italic;">// decode our content file</span>
&nbsp;
				<span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filepath'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> file_directory_path<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$file_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base64_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// set content type db table, and field to search on.</span>
				<span style="color: #666666; font-style: italic;">// Load up the CCK field</span>
&nbsp;
				<span style="color: #000088;">$filetype</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filetype</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'pdf'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$content_table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'content_type_homes'</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$fid_field</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'field_floorplan_fid'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$content_table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'content_field_image'</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$fid_field</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'field_image_fid'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #000088;">$file_details</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// check if it's the same file coming over</span>
				<span style="color: #666666; font-style: italic;">// only applies if the node exists in the first place. otherwise its a new import on a new home</span>
				<span style="color: #666666; font-style: italic;">// if there is a file match we dont want to do anything but skip the creation</span>
				<span style="color: #666666; font-style: italic;">// i think if you dont keep the node-&gt;file field prefilled, and returns those fields empty, it causes the file deletion on the server</span>
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> db_query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT f.filename, f.fid, f.filepath, f.timestamp FROM {files} f
											INNER JOIN {<span style="color: #009933; font-weight: bold;">%s</span>} ctype ON f.fid = ctype.<span style="color: #009933; font-weight: bold;">%s</span>
										WHERE f.filename = '<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content_table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fid_field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
					<span style="color: #000088;">$file_details</span> <span style="color: #339933;">=</span> db_fetch_array<span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_details</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$file_details</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">// if file exists and has not changed remotely, skip it</span>
&nbsp;
					watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'homes_sync'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@file already exists for @home. Skipping.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@file'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$file_details</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@home'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> WATCHDOG_NOTICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> 
&nbsp;
					<span style="color: #666666; font-style: italic;">// save file</span>
					<span style="color: #666666; font-style: italic;">// REPLACE because we can be certain we will not have duplicate files</span>
					<span style="color: #666666; font-style: italic;">// so we will only ever receive unique ones here</span>
&nbsp;
					file_save_data<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filepath'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> FILE_EXISTS_REPLACE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">//write record in files table</span>
					drupal_write_record<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'files'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> db_query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT fid,filepath FROM {files} WHERE filename = '<span style="color: #009933; font-weight: bold;">%s</span>'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$file_details</span> <span style="color: #339933;">=</span> db_fetch_object<span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #000088;">$file_data</span> <span style="color: #339933;">=</span> field_file_load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_details</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'homes_sync'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Uploaded @file into @home.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@file'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$full_file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'@home'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> WATCHDOG_NOTICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
						<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$filetype</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'pdf'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
							<span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_floorplan</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file_data</span><span style="color: #339933;">;</span>
						<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
							<span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">field_image</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$file_data</span><span style="color: #339933;">;</span>
						<span style="color: #009900;">&#125;</span>
&nbsp;
					<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$node</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>After the file is saved to the file system and database, <a href="http://api.lullabot.com/field_file_load" target="_blank">field_file_load()</a> is called to load the file data. With a populated array now, we can add that to our node object by simply putting $node->field_floorplan[] = $file_data or $node->field_image[] = $file_data to append the existing array with data. The $node object is returned, and node_save($node) is immediately called, and our node data and file data are saved. Just like that, we have a simple content distribution system from one parent site to potentially many remote sites.</p>
<p>This is just a simple example, your mileage may vary. We are utilizing this technology to hook companies together under different Drupal installations on different servers. Using <a href="http://www.drupal.org/project/services" target="_blank">Services</a> you could also distribute <a href="http://www.drupal.org/project/views" target="_blank">Views</a> configurations, users, and system settings.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.delawarewebdesigner.com/how-tos/sync-files-in-drupal-using-services-and-xmlrpc.htm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
