<?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>Moki Systems Blog &#187; PHP</title>
	<atom:link href="http://www.mokisystems.com/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mokisystems.com/blog</link>
	<description>Addressing such topics as: web programming, design, ruby on rails, cake php, postgresql, linux, seo</description>
	<lastBuildDate>Fri, 20 May 2011 15:22:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ajax multi file upload with php, iframe, &amp; javascript</title>
		<link>http://www.mokisystems.com/blog/ajax-multi-file-upload-with-php-iframe-javascript/</link>
		<comments>http://www.mokisystems.com/blog/ajax-multi-file-upload-with-php-iframe-javascript/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 22:08:35 +0000</pubDate>
		<dc:creator>andrew</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javaxcript]]></category>
		<category><![CDATA[multi-file upload]]></category>

		<guid isPermaLink="false">http://www.mokisystems.com/blog/?p=28</guid>
		<description><![CDATA[I found several solutions after browsing around google trying to find a multi-file uploader built around php.  Most of these solutions for multi-file uploading fit into one of these three categories.

Use Flash to upload the images.  (The browser only supports one image at a time) &#8211; SWFUpload
Use Javascript to add multiple file type [...]]]></description>
			<content:encoded><![CDATA[<p>I found several solutions after browsing around google trying to find a multi-file uploader built around php.  Most of these solutions for multi-file uploading fit into one of these three categories.</p>
<ol>
<li>Use Flash to upload the images.  (The browser only supports one image at a time) &#8211; <a href="http://swfupload.org/">SWFUpload</a></li>
<li>Use Javascript to add multiple file type fields, while hidding previous fields &#8211; <a href="http://the-stickman.com/web-development/javascript/multiple-file-uploader-mootools-version/">the-stickman</a></li>
<li>Use Javascript and hidden iframes embedded on the same page to upload the images. &#8211; <a href="http://www.ajaxf1.com/tutorial/ajax-file-upload-tutorial.html">ajaxf1</a></li>
</ol>
<p>I decided to take pieces of the mentioned methods and come up with a solution that worked for me.</p>
<p>Here is the idea behind what we are going to be doing.</p>
<ol>
<li>Create a HTML form which has a file upload field</li>
<li>Set the target of this HTML form to an iFrame which is on the same page</li>
<li>Have Javascript submit the form everytime the &#8220;file&#8221; field changes</li>
<li>Javascript hides the form when submitted, and displays an &#8220;animation&#8221;</li>
<li>PHP takes the uploaded file, thumbnails it, and stores the image data</li>
<li>Javascript unhides the form, and re-hides the animation when upload is complete</li>
<li>Javascript and php show the thumbnail on the same page right after it was uploaded</li>
</ol>
<p>This makes this little script appear like an ajax application.  It is NOT actually AJAX, but uses css, iframes, php sessions, and javascript to make it appear so.</p>
<p>I&#8217;ll try to explain what is happening at each step, but keep in mind, I&#8217;m not an expert programmer like my fellow co-workers, and I borrowed most of the work from the mentioned sources above.  If you just want to download all the files and take the easy way out, skip to the bottom of the post.</p>
<p><strong>Steps 1 &#038; 2 &#038; 3 &#8211; Make Our Form</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript"><span style="color: #339933;">&lt;</span>form <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;pictureform&quot;</span> action<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;upload.php&quot;</span> method<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;post&quot;</span> enctype<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;multipart/form-data&quot;</span> target<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;upload_target&quot;</span> <span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>p id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;f1_upload_process&quot;</span><span style="color: #339933;">&gt;</span>Loading...<span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;loader.gif&quot;</span> <span style="color: #339933;">/&gt;&lt;</span>br<span style="color: #339933;">/&gt;&lt;/</span>p<span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>p id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;f1_upload_form&quot;</span> align<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;center&quot;</span><span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;</span>
         <span style="color: #339933;">&lt;</span>label<span style="color: #339933;">&gt;</span>Add Pictures<span style="color: #339933;">:</span>  
              <span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;myfile&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;file&quot;</span> size<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;30&quot;</span> onchange<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;startUpload();document.pictureform.submit();&quot;</span> <span style="color: #339933;">/&gt;</span>
         <span style="color: #339933;">&lt;/</span>label<span style="color: #339933;">&gt;</span>					 
     <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>iframe id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;upload_target&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;upload_target&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;#&quot;</span> style<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;width:0;height:0;border:0px solid #fff;&quot;</span><span style="color: #339933;">&gt;&lt;/</span>iframe<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><span id="more-28"></span><br />
Explanation of the mentioned code:<br />
<code>enctype="multipart/form-data"</code><br />
Tells the browser you are submitting files, or images with your form.</p>
<p><code>onchange="startUpload();document.pictureform.submit();</code><br />
The onchange event of the input field calls the function startUpload(), which will hide the form, and show the animation while uploading.  The document.pictureform.submit(); Submits the form to our &#8220;action&#8221; which we have as &#8220;upload.php&#8221;</p>
<p><strong>Step 4 &#8211; Make the JavaScript functions for hiding and showing</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> startUpload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'visible'</span><span style="color: #339933;">;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hidden'</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Explanation of the mentioned code:<br />
By giving our HTML elements id tags,such as <code>id=f1_upload_process</code><br />
we make them available for manipulation by JavaScript via the DOM.</p>
<p><code>document.getElementById('f1_upload_process').style.visibility = 'visible';</code><br />
says: &#8220;get the element in the document that has an id of &#8220;f1_upload_process&#8221; and make it visible.&#8221;</p>
<p>This then turns on the visibility of our &#8220;animation&#8221;
<p> block which we have set &#8220;invisible&#8221; by default in our CSS file.  The opposite occurs for the form block, we make it &#8220;invisible&#8221; or &#8220;hidden.&#8221;</p>
<p>Step 5 &#8211; Create our &#8220;upload.php&#8221; script to handle thumbnail and copy image to filesystem</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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #666666; font-style: italic;">// keep our session</span>
	<span style="color: #990000;">session_start</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 the upload file type</span>
&nbsp;
	<span style="color: #000033;">$filetype</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000033;">$gd_function_suffix</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>  
 		<span style="color: #0000ff;">'image/pjpeg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'JPEG'</span><span style="color: #339933;">,</span> 
 		<span style="color: #0000ff;">'image/jpeg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'JPEG'</span><span style="color: #339933;">,</span> 
 		<span style="color: #0000ff;">'image/gif'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'GIF'</span><span style="color: #339933;">,</span> 
 		<span style="color: #0000ff;">'image/bmp'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'WBMP'</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'image/png'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'PNG'</span><span style="color: #339933;">,</span>
 		<span style="color: #0000ff;">'image/x-png'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'PNG'</span> 
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$function_suffix</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$gd_function_suffix</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$filetype</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Build Function name for ImageCreateFromSUFFIX </span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$function_suffix</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Unsupported file type, must be JPG|PNG|GIF|BMP&quot;</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: #000033;">$function_to_read</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ImageCreateFrom'</span> <span style="color: #339933;">.</span> <span style="color: #000033;">$function_suffix</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the image and create a thumbnail</span>
	<span style="color: #000033;">$img</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$function_to_read</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'myfile'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tmp_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$img</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span>  <span style="color: #0000ff;">&quot;could not create image handle&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000033;">$width</span> <span style="color: #339933;">=</span> imageSX<span style="color: #009900;">&#40;</span><span style="color: #000033;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Get the image width</span>
	<span style="color: #000033;">$height</span> <span style="color: #339933;">=</span> imageSY<span style="color: #009900;">&#40;</span><span style="color: #000033;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Get the image height</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$width</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000033;">$height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span>  <span style="color: #0000ff;">&quot;No Width or Height&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Build the thumbnail</span>
	<span style="color: #000033;">$target_width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Thumnail width</span>
	<span style="color: #000033;">$target_height</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Thumbnail height</span>
	<span style="color: #000033;">$target_ratio</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_width</span> <span style="color: #339933;">/</span> <span style="color: #000033;">$target_height</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//New target ratio</span>
&nbsp;
	<span style="color: #000033;">$img_ratio</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$width</span> <span style="color: #339933;">/</span> <span style="color: #000033;">$height</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Original ratio</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$target_ratio</span> <span style="color: #339933;">&gt;</span> <span style="color: #000033;">$img_ratio</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$new_height</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_height</span><span style="color: #339933;">;</span>
		<span style="color: #000033;">$new_width</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$img_ratio</span> <span style="color: #339933;">*</span> <span style="color: #000033;">$target_height</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Tall Image</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$new_height</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_width</span> <span style="color: #339933;">/</span> <span style="color: #000033;">$img_ratio</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Wide Image</span>
		<span style="color: #000033;">$new_width</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_width</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_height</span> <span style="color: #339933;">&gt;</span> <span style="color: #000033;">$target_height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$new_height</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_height</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_width</span> <span style="color: #339933;">&gt;</span> <span style="color: #000033;">$target_width</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$new_height</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$target_width</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000033;">$new_img</span> <span style="color: #339933;">=</span> ImageCreateTrueColor<span style="color: #009900;">&#40;</span><span style="color: #000033;">$target_width</span><span style="color: #339933;">,</span> <span style="color: #000033;">$target_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Create a new image handle for thumbnail</span>
	<span style="color: #000033;">$white</span> <span style="color: #339933;">=</span> imagecolorallocate<span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_img</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Allocate the color white</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!@</span>imagefilledrectangle<span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_img</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000033;">$target_width</span><span style="color: #cc66cc;">-1</span><span style="color: #339933;">,</span> <span style="color: #000033;">$target_height</span><span style="color: #cc66cc;">-1</span><span style="color: #339933;">,</span> <span style="color: #000033;">$white</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;">// Fill the image white</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Couldn't fill image&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// Copy the uploaded image to the newly created image</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!@</span>imagecopyresampled<span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_img</span><span style="color: #339933;">,</span> <span style="color: #000033;">$img</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$target_width</span><span style="color: #339933;">-</span><span style="color: #000033;">$new_width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$target_height</span><span style="color: #339933;">-</span><span style="color: #000033;">$new_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000033;">$new_width</span><span style="color: #339933;">,</span> <span style="color: #000033;">$new_height</span><span style="color: #339933;">,</span> <span style="color: #000033;">$width</span><span style="color: #339933;">,</span> <span style="color: #000033;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;couldn't copy new image&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Create php Session array for image information</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</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>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Use a output buffering to load the image into a variable</span>
	<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	imagejpeg<span style="color: #009900;">&#40;</span><span style="color: #000033;">$new_img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000033;">$imagevariable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">ob_end_clean</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;">// Create a unique id or name for the image</span>
	<span style="color: #000033;">$file_id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;myfile&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tmp_name&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000033;">$new_name</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$file_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.$function_suffix&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000033;">$temp_name</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$_FILES</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;myfile&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;tmp_name&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// assign upload directory, and target path for image</span>
	<span style="color: #000033;">$uploaddir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getcwd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//</span>
	<span style="color: #000033;">$target_path</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$uploaddir</span><span style="color: #339933;">.</span>DIRECTORY_SEPARATOR<span style="color: #339933;">.</span><span style="color: #000033;">$new_name</span><span style="color: #339933;">;</span>
&nbsp;
 	<span style="color: #666666; font-style: italic;">// Move uploaded image to uploaddir location</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!@</span><span style="color: #990000;">move_uploaded_file</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$temp_name</span><span style="color: #339933;">,</span> <span style="color: #000033;">$target_path</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$err</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Couldn't Copy File to Filesystem&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// Create a Session array with name, type, and image object for use in displaying thumbnails</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$file_id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$file_id</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>										<span style="color: #0000ff;">&quot;name&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$new_name</span><span style="color: #339933;">,</span> 								<span style="color: #0000ff;">&quot;type&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$function_suffix</span><span style="color: #339933;">,</span> 
						<span style="color: #0000ff;">&quot;obj&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$imagevariable</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: #666666; font-style: italic;">// If no error, then pass good result and file id to Javascript for generating the thumbnail.</span>
   <span style="color: #666666; font-style: italic;">// Pass our $err varialbe along</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000033;">$err</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;'1','&quot;</span><span style="color: #339933;">.</span><span style="color: #000033;">$file_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</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: #000033;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;'.$err.','&quot;</span><span style="color: #339933;">.</span><span style="color: #000033;">$file_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
   	<span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// in case of really fast upload, sleep script to show loading animation for a bit longer</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Javascript calls the &quot;stopUpload&quot; function with its paramaters</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;script language</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>window<span style="color: #339933;">.</span>top<span style="color: #339933;">.</span>window<span style="color: #339933;">.</span>stopUpload<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">echo</span> <span style="color: #000033;">$result</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Explanation of Code:<br />
These php functions make use of the GD library.  You must have that enabled in PHP in order to use this.  Also make sure your current directory is writeable.</p>
<p>I won&#8217;t explain to many things here, as the code has inline comments.  You could make this code a function and have it thumbnail and save multiple sizes to the filesytem.  Currently it simply thumbnails a 100  X 100 and copies the original to the current directory the script is running from.  It will do GIF, JPEG, BMP, or PNG.  Error checking and Session handling could be improved.</p>
<p><strong>Step 6 &#8211; Create JavaScript function to show form, and hide animation when upload is finished</strong></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
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> stopUpload<span style="color: #009900;">&#40;</span>success<span style="color: #339933;">,</span>fileid<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>success <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         result <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;span class=&quot;msg&quot;&gt;The file was uploaded successfully!&lt;<span style="color: #000099; font-weight: bold;">\/</span>span&gt;&lt;br/&gt;&lt;br/&gt;'</span><span style="color: #339933;">;</span>
		 addImage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;thumbnail.php?id=&quot;</span> <span style="color: #339933;">+</span> fileid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
         result <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;span class=&quot;emsg&quot;&gt;There was an error during file upload!'</span> <span style="color: #339933;">+</span> success <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;<span style="color: #000099; font-weight: bold;">\/</span>span&gt;&lt;br/&gt;&lt;br/&gt;'</span><span style="color: #339933;">;</span> 
      <span style="color: #009900;">&#125;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_process'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hidden'</span><span style="color: #339933;">;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">innerHTML</span> <span style="color: #339933;">=</span> result <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;label&gt;File: &lt;input name=&quot;myfile&quot; type=&quot;file&quot; size=&quot;30&quot;  onchange=&quot;startUpload();document.pictureform.submit();&quot; /&gt;&lt;<span style="color: #000099; font-weight: bold;">\/</span>label&gt;'</span><span style="color: #339933;">;</span>
      document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'f1_upload_form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">visibility</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'visible'</span><span style="color: #339933;">;</span>      
&nbsp;
	  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>   
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Explanation of Code:<br />
From our upload.php script, we passed two varialbes to this JavaScript function, Success and File Id.<br />
If we had a successful file upload, we throw up a message saying &#8220;success&#8221; and call another function called <code> addImage("thumbnail.php?id=" + fileid);</code> <- More on this later...</p>
<p>If we didn't have a successful upload, we return a message saying so.</p>
<p>The last few lines of the function hide the "animation" bar, and recreate our form input element so we can upload another image, and do the whole thing over again.  All without leaving the page!</p>
<p><strong>Step 7 &#8211; Our Thumbnail.php script and our addImage() Javascript function</strong></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
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #666666; font-style: italic;">// This script accepts an ID and looks in the user's session for stored thumbnail data.</span>
	<span style="color: #666666; font-style: italic;">// It then streams the data to the browser as an image	</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000033;">$image_id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000033;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the id from the URL</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000033;">$image_id</span> <span style="color: #339933;">===</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$image_id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: image/jpeg&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$image_id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">echo</span> <span style="color: #000033;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;file_info&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$image_id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>obj<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Code Explanation:<br />
This bit of code simply gets the image id from the URL and pulls the image thumbnail data we saved into our session in our upload.php script, and outputs the image to the browser.</p>
<p><strong>addImage() Javascript Function</strong></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
</pre></td><td class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> addImage<span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> newImg <span style="color: #339933;">=</span> document.<span style="color: #006600;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	newImg.<span style="color: #006600;">style</span>.<span style="color: #006600;">margin</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;5px&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	document.<span style="color: #006600;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;thumbnails&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">appendChild</span><span style="color: #009900;">&#40;</span>newImg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newImg.<span style="color: #006600;">filters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			newImg.<span style="color: #006600;">filters</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DXImageTransform.Microsoft.Alpha&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">opacity</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.</span>
			newImg.<span style="color: #006600;">style</span>.<span style="color: #006600;">filter</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'progid:DXImageTransform.Microsoft.Alpha(opacity='</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		newImg.<span style="color: #006600;">style</span>.<span style="color: #006600;">opacity</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	newImg.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		fadeIn<span style="color: #009900;">&#40;</span>newImg<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	newImg.<span style="color: #006600;">src</span> <span style="color: #339933;">=</span> src<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> fadeIn<span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> opacity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> reduceOpacityBy <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> rate <span style="color: #339933;">=</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">;</span>	<span style="color: #006600; font-style: italic;">// 15 fps</span>
&nbsp;
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opacity <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		opacity <span style="color: #339933;">+=</span> reduceOpacityBy<span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opacity <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			opacity <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>element.<span style="color: #006600;">filters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				element.<span style="color: #006600;">filters</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;DXImageTransform.Microsoft.Alpha&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">opacity</span> <span style="color: #339933;">=</span> opacity<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.</span>
				element.<span style="color: #006600;">style</span>.<span style="color: #006600;">filter</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'progid:DXImageTransform.Microsoft.Alpha(opacity='</span> <span style="color: #339933;">+</span> opacity <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			element.<span style="color: #006600;">style</span>.<span style="color: #006600;">opacity</span> <span style="color: #339933;">=</span> opacity <span style="color: #339933;">/</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>opacity <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			fadeIn<span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> opacity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> rate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Code Explanation:<br />
I borrowed most of this code from the SWFUpload project.  The basic idea is that we create a new image tag via javascript, give it a src of &#8220;thumbnail.php?id=XXXXX&#8221; XXXXX being our image name, and call a &#8220;fadeIn&#8221; function which does just that, fade the image in for a lovely effect.  I have this javascript included in my index.php page via</p>

<div class="wp_syntax"><div class="code"><pre>&lt;script type=&quot;text/javascript&quot; src=&quot;handlers.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>Thats it!  Hope you learned as much as I did from this tutorial.</p>
<p><em>Important Notes:</em></p>
<p>-This script does not check file size, or limit the number of files that can be uploaded.<br />
-You will need to add in appropriate error checking on the uploaded files for security.<br />
-You will want to clean out the data from the session when done with the uploading.<br />
-Make sure you have write permission the directory where images are being saved<br />
-You must have GD library installed for this script to work</p>
<p>All the files you need to be up and running are found here -> <a href='http://www.mokisystems.com/blog/wp-content/uploads/2008/07/ajax-file-upload-mokisystems.zip'>ajax-file-upload-mokisystems</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mokisystems.com/blog/ajax-multi-file-upload-with-php-iframe-javascript/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

