July 22nd, 2008 By: andrew
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) –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
Explanation of the mentioned code:
enctype="multipart/form-data"
Tells the browser you are submitting files, or images with your form.
onchange="startUpload();document.pictureform.submit();
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 “action” which we have as “upload.php”
Step 4 – Make the JavaScript functions for hiding and showing
Explanation of the mentioned code:
By giving our HTML elements id tags,such as
we make them available for manipulation by JavaScript via the DOM.
By giving our HTML elements id tags,such as
id=f1_upload_process
we make them available for manipulation by JavaScript via the DOM.
document.getElementById('f1_upload_process').style.visibility = 'visible';
says: “get the element in the document that has an id of “f1_upload_process” and make it visible.”
This then turns on the visibility of our “animation”
block which we have set “invisible” by default in our CSS file. The opposite occurs for the form block, we make it “invisible” or “hidden.”
Step 5 – Create our “upload.php” script to handle thumbnail and copy image to filesystem
Explanation of Code:
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.
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.
I won’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.
Step 6 – Create JavaScript function to show form, and hide animation when upload is finished
Explanation of Code:
From our upload.php script, we passed two varialbes to this JavaScript function, Success and File Id.
If we had a successful file upload, we throw up a message saying “success” and call another function called
From our upload.php script, we passed two varialbes to this JavaScript function, Success and File Id.
If we had a successful file upload, we throw up a message saying “success” and call another function called
addImage("thumbnail.php?id=" + fileid);
<- More on this later...
If we didn't have a successful upload, we return a message saying so.
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!
Step 7 – Our Thumbnail.php script and our addImage() Javascript function
Code Explanation:
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.
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.
addImage() Javascript Function
Code Explanation:
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 “thumbnail.php?id=XXXXX” XXXXX being our image name, and call a “fadeIn” function which does just that, fade the image in for a lovely effect. I have this javascript included in my index.php page via
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 “thumbnail.php?id=XXXXX” XXXXX being our image name, and call a “fadeIn” function which does just that, fade the image in for a lovely effect. I have this javascript included in my index.php page via
Thats it! Hope you learned as much as I did from this tutorial.
Important Notes:
-This script does not check file size, or limit the number of files that can be uploaded.
-You will need to add in appropriate error checking on the uploaded files for security.
-You will want to clean out the data from the session when done with the uploading.
-Make sure you have write permission the directory where images are being saved
-You must have GD library installed for this script to work
-You will need to add in appropriate error checking on the uploaded files for security.
-You will want to clean out the data from the session when done with the uploading.
-Make sure you have write permission the directory where images are being saved
-You must have GD library installed for this script to work
All the files you need to be up and running are found here -> ajax-file-upload-mokisystems
July 31st, 2008 at 9:56 am
Sean Meyer
July 31st, 2008 at 10:14 am
header("Content-Length: ".strlen($_SESSION["file_info"][$image_id]));
Becomes
//header("Content-Length: ".strlen($_SESSION["file_info"][$image_id]));
July 31st, 2008 at 1:04 pm
August 4th, 2008 at 7:39 pm
August 4th, 2008 at 8:46 pm
$thumb_name = $file_id.”_th.$function_suffix”;
imagejpeg($new_img, $thumb_name);
if(!@move_uploaded_file($thumb_name, $target_path)){
$err = “Couldn’t Copy Thumbnail to Filesystem”;
}
September 30th, 2008 at 4:50 am
January 7th, 2009 at 3:51 am
January 7th, 2009 at 5:12 am
try to upload more than 2 images with no error. i can’t
January 20th, 2009 at 2:52 pm
March 1st, 2009 at 1:10 am
March 17th, 2009 at 3:02 am
June 9th, 2009 at 10:51 pm
Can you please help me on how to retrict to upload only 4 or 5 etc., files.
October 27th, 2009 at 9:15 am
July 21st, 2010 at 6:04 pm
Hope you do more of these, this was fun…
January 11th, 2011 at 11:34 pm
June 2nd, 2011 at 1:02 pm