Follow Me

Dynamic multiple image upload using jquery

Sun, Jun 27, 2010

JQuery, PHP

Friends, Today I am posting multiple image upload using jquery in a php form. It is a dynamic script that will upload image without refresh and stored temporarily in the database. It will finally set in database when we will submit our main form.

Here is the code for our form -

<table width="100%" align="center" border="0">
<form method="post" enctype="multipart/form-data">
<tr>
	<td width="25%">Title :</td>
	<td><input type="text" name="title" id="title" size="30" value=""/></td>
</tr>

<tr>
	<td valign="top">Item Avatar :</td><td valign="top">
	<div id="upload" ><span>Upload Image<span></div><span id="status"></span>
	<table><tr><td id="files"></td></tr></table>
	</td>
</tr>
<tr>
	<td colspan="2" align="center">
	<input type="submit" name="add_item" value="Submit" id="sub_button" />&nbsp;
       </td>
</tr>
</form>
</table>

We will use this simple jquery code for sending server side request to store our image as well as client side validation for valid image.

<script language="javascript" type="text/javascript">
$(function()
{
	var btnUpload=$('#upload');
	var status=$('#status');
	new AjaxUpload(btnUpload, {
	action: 'upload-file.php',
	name: 'uploadfile',
	onSubmit: function(file, ext)
	{
	 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
     // extension is not allowed
	status.text('Only JPG, PNG or GIF files are allowed');
	return false;
	}status.text('Uploading...');
	},

	onComplete: function(file, response)
	{
		//On completion clear the status
		status.text('');
		//Add uploaded file to list
		var bb=response.substr(0,7)
		var idd=response.replace('success',' ');
		var idb =idd.replace(/^\s*|\s*$/g,'');
		if(bb==="success")
		{
			$('<span></span>').appendTo('#files').html('<img src="images/'+file+'" alt="" width="120" height="120" style="margin:5px;" />').addClass('success');
		}
		else
		{
			$('<span></span>').appendTo('#files').text(file).addClass('error');
		}
	}});
});

</script>

In upload-file.php, we simply write code to upload file using php function move_uploaded_file() using temp session variable for first time. After uploading no. of files, when we finally submit form data then we replace temp session with the main table id in database at the time of insert data to table.

You can download complete source code from here.

Download

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
, , , ,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • 11 Comments For This Post

    1. Persa Says:

      Hi,
      very nice And 100% Work. But i have 2 problem .
      1- How To add limit for upload images .
      2- How To add remove link for each image.
      Thanks

    2. admin Says:

      Thanks Persa,

      I will upload post again with requested features so that all users can also get updates.

    3. kolitha Says:

      Your code is very important. I need to upload more files with two input fields it mean when form load I need to make 5 upload fields. I try to improve your code but am unable to develop to upload more than one file buttons. Please help me?

    4. phptechie Says:

      It’s Ok , Better create the image upload…

      Thanks,

    5. Dave Says:

      I am trying to save image links to a mysql database from upload-file.php, however no cookies or sessions will carry across to this page, any ideas? Basically i need to have a user_id parse accross

    6. admin Says:

      @Dave While receiving response on index page, you can append one text field for image link. You can retrieve and insert this link into db after posting.

    7. kiran Says:

      i have d/w the plugin n integrated it …but the image file is not getting saved…it is going in the error condition ..please help

    8. admin Says:

      @kiran Make sure you have given proper permission to image folder.

    9. Sib Says:

      Thanks, this looks good.
      Now if only I can position it the right way, in my DIV’s.

    10. Simon Says:

      This script looks great and I have already started testing it for a future site.

      One important thing missing is file size, you should be able to set a max file size option that will stop the code if that size is exceeded or perhaps a way of reducing the file size of a larger file.

    11. admin Says:

      @Simon, thanks.. file size validation can be integrate. I will soon publish its third updated version with more features.

      you can try image resize into it.. just read below post..

      http://smartcoderszone.com/2010/05/simple-image-resize-script-in-php/

    Leave a Reply