Ajax multi file upload with php, iframe, & javascript
Tuesday, July 22nd, 2008 By: andrewI 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) - SWFUpload
- Use Javascript to add multiple file type fields, while hidding previous fields - the-stickman
- Use Javascript and hidden iframes embedded on the same page to upload the images. - ajaxf1
I decided to take pieces of the mentioned methods and come up with a solution that worked for me.
Here is the idea behind what we are going to be doing.
- Create a HTML form which has a file upload field
- Set the target of this HTML form to an iFrame which is on the same page
- Have Javascript submit the form everytime the “file” field changes
- Javascript hides the form when submitted, and displays an “animation”
- PHP takes the uploaded file, thumbnails it, and stores the image data
- Javascript unhides the form, and re-hides the animation when upload is complete
- Javascript and php show the thumbnail on the same page right after it was uploaded
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.
I’ll try to explain what is happening at each step, but keep in mind, I’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.
Steps 1 & 2 & 3 - Make Our Form
1 2 3 4 5 6 7 8 9 | <form name="pictureform" action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" > <p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p> <p id="f1_upload_form" align="center"><br/> <label>Add Pictures: <input name="myfile" type="file" size="30" onchange="startUpload();document.pictureform.submit();" /> </label> </p> <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe> </form> |

Recently Moki Systems was invited to teach, at ...