<?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>Cardiff web site design, Michael Gareth Morgan &#187; Software development</title>
	<atom:link href="http://www.michaelgarethmorgan.com/blog/software-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michaelgarethmorgan.com</link>
	<description>Cardiff web site designer</description>
	<lastBuildDate>Fri, 01 Apr 2011 07:47:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Upload an image using Java + PHP</title>
		<link>http://www.michaelgarethmorgan.com/upload-an-image-using-java-php/</link>
		<comments>http://www.michaelgarethmorgan.com/upload-an-image-using-java-php/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 12:01:31 +0000</pubDate>
		<dc:creator>Michael Morgan</dc:creator>
				<category><![CDATA[Software development]]></category>

		<guid isPermaLink="false">http://www.michaelgarethmorgan.com/?p=302</guid>
		<description><![CDATA[Whilst working on a recent project I needed a way of transferring images (screenshots to be specific) from the client machine to a web server hosted in the cloud. My choice of platform was Java on the client machine and then PHP on the server side to recieve and process the upload. So basically I [...]]]></description>
			<content:encoded><![CDATA[<p>Whilst working on a recent project I needed a way of transferring images (screenshots to be specific) from the client machine to a web server hosted in the cloud. My choice of platform was Java on the client machine and then PHP on the server side to recieve and process the upload.</p>
<p>So basically I needed to upload an image to a web server using Java and PHP. Reading and writing simple data to and from a web server with Java is pretty easy really but when it comes to transferring files things get that little bit tougher. This is because when uploading files the content type of the HTTP request changes from plain text to multipart making it trickier. You could either write a whole load of extra code to deal with this or you could simply send the data over as plain text and then just let PHP deal with it. Here&#8217;s how it&#8217;s done.<span id="more-302"></span></p>
<h3>The Java</h3>

<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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Setup vars</span>
<span style="color: #003399;">HttpURLConnection</span> httpUrlConnection<span style="color: #339933;">;</span>
<span style="color: #003399;">OutputStream</span> outputStream<span style="color: #339933;">;</span>
<span style="color: #003399;">BufferedInputStream</span> fileInputStream<span style="color: #339933;">;</span>
<span style="color: #003399;">BufferedReader</span> serverReader<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> totalBytes<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> bytesTrasferred<span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> response <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> serverResponse <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> localFileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mypicture.jpg&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Establish a connection</span>
httpUrlConnection<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.example.com/upload.php&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
httpUrlConnection.<span style="color: #006633;">setDoOutput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
httpUrlConnection.<span style="color: #006633;">setRequestMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
outputStream <span style="color: #339933;">=</span> httpUrlConnection.<span style="color: #006633;">getOutputStream</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;">// Buffered input stream</span>
fileInputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>localFileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the size of the image</span>
totalBytes <span style="color: #339933;">=</span> fileInputStream.<span style="color: #006633;">available</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;">// Loop through the files data</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> totalBytes<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Write the data to the output stream</span>
	outputStream.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>fileInputStream.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	bytesTrasferred <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Close the output stream</span>
outputStream.<span style="color: #006633;">close</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;">// New reader to get server response</span>
serverReader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>httpUrlConnection.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Read the servers response</span>
serverResponse <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>response <span style="color: #339933;">=</span> serverReader.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	serverResponse <span style="color: #339933;">=</span> serverResponse <span style="color: #339933;">+</span> response<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Close the buffered reader</span>
serverReader.<span style="color: #006633;">close</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;">// Close the file input stream</span>
fileInputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>The PHP</h3>

<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Config</span>
<span style="color: #000088;">$uploadBase</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;img/&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$uploadFilename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.jpg&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$uploadPath</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$uploadBase</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$uploadFilename</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Upload directory</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uploadBase</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uploadBase</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Grab the data</span>
<span style="color: #000088;">$incomingData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php://input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Valid data?</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$incomingData</span><span style="color: #009900;">&#41;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No input data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Write to disk</span>
<span style="color: #000088;">$fh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uploadPath</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error opening file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #339933;">,</span> <span style="color: #000088;">$incomingData</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error writing to file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error closing file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Success&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And that&#8217;s all there is. By placing the PHP script above onto a suitable web server and then executing the Java code you&#8217;ll be able to quite easily and painlessly transfer images (or any other file types) from the desktop client to the web.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaelgarethmorgan.com/upload-an-image-using-java-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Isometric PHP</title>
		<link>http://www.michaelgarethmorgan.com/isometric-php/</link>
		<comments>http://www.michaelgarethmorgan.com/isometric-php/#comments</comments>
		<pubDate>Tue, 25 May 2010 12:27:31 +0000</pubDate>
		<dc:creator>Michael Morgan</dc:creator>
				<category><![CDATA[Software development]]></category>

		<guid isPermaLink="false">http://www.michaelgarethmorgan.com/?p=249</guid>
		<description><![CDATA[GD and PHP make a great couple and when used properly they can produce some fantastic results. Purely as an experiment to see how well it would work I&#8217;ve been playing with some isometric graphics in PHP. It&#8217;s not much but I&#8217;ve just created the following isometric scene. It&#8217;s quite simple really; a grid made [...]]]></description>
			<content:encoded><![CDATA[<p>GD and PHP make a great couple and when used properly they can produce some fantastic results. Purely as an experiment to see how well it would work I&#8217;ve been playing with some isometric graphics in PHP. It&#8217;s not much but I&#8217;ve just created the following isometric scene.</p>
<p><a href="http://files.michaelgarethmorgan.com/isometric-php.png"><img src="http://files.michaelgarethmorgan.com/isometric-php-530x265.png" alt="Isometric PHP" title="Isometric PHP" width="530" height="265" class="aligncenter size-medium wp-image-252" /></a></p>
<p>It&#8217;s quite simple really; a grid made up of diamonds pieced together to form the grid lines. Then, another simple isometric box shape placed somewhere in the middle. With this approach each grid section can be individually changed &#8211; this means that if enough time was spent it would be possible (and not all that difficult) to create a PHP + GD script which would generate random landscapes or cities. I quite like that idea, if only I had the time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaelgarethmorgan.com/isometric-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Learning Objective-C</title>
		<link>http://www.michaelgarethmorgan.com/learning-objective-c/</link>
		<comments>http://www.michaelgarethmorgan.com/learning-objective-c/#comments</comments>
		<pubDate>Sat, 22 May 2010 19:17:46 +0000</pubDate>
		<dc:creator>Michael Morgan</dc:creator>
				<category><![CDATA[Software development]]></category>

		<guid isPermaLink="false">http://www.michaelgarethmorgan.com/?p=246</guid>
		<description><![CDATA[Though I would never say that I have mastered a particular language I think I can safely say that I am fairly confident when it comes to XHTML, CSS, JavaScript and PHP. I&#8217;m also relatively confident with developing on Java. So what&#8217;s next? A few months ago I bought an Intel Mac Mini with the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.michaelgarethmorgan.com/learning-objective-c/"><img src="http://files.michaelgarethmorgan.com/iphone.png" alt="Objective-C iPhone developmenta" title="Objective-C iPhone developmenta" width="128" height="128" class="alignright size-full wp-image-247" /></a>Though I would never say that I have mastered a particular language I think I can safely say that I am fairly confident when it comes to XHTML, CSS, JavaScript and PHP. I&#8217;m also relatively confident with developing on Java. So what&#8217;s next?</p>
<p>A few months ago I bought an Intel Mac Mini with the sole intention of developing applications for the iPhone. This meant I&#8217;d not only have to learn the Objective-C language but would also have to come to grips with Mac OSX &#8211; an OS which I&#8217;m not at all used to. After putting it off for a little while I&#8217;ve finally decided to take the leap and have begun the learning process.</p>
<p>To help this learning process I&#8217;ll try to document what particular things I&#8217;ve been looking at as I progress through what seems to be a very steep learning curve. Therefore from time to time I&#8217;ll post the occasional blog post explaining some of the things I&#8217;ve learnt with regards to Objective-C and iPhone application development. As well as being a great way for me to remember what I&#8217;ve been doing it may also be of some use to anyone else looking to take on this challenge.</p>
<p>Wish me luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaelgarethmorgan.com/learning-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
