jQuery Upload Multiple Images In PHP

This tutorial is written in PHP and jQuery. We create this project and named as jQuery Upload Multiple Images In PHP. Every file contains post upload process and move the files in uploads directory. First check directory and error after that check if single file coming then process separate if multiple files handle in loop. You will see all the files that you uploaded in the directory folder. This simple application will bring to you a very big help to your projects and add it to your features to make it more attractive and responsive to every users. See the example code below.

Sample Code

Index.html - This file contains jQuery library and CSS script to handle the requests.
  1. <!DOCTYPE html>
  2. <title>jQuery Upload Multiple Images In PHP</title>
  3. <link href="css/uploadfilemulti.css" rel="stylesheet">
  4. <script src="js/jquery-1.8.0.min.js"></script>
  5. <script src="js/jquery.fileuploadmulti.min.js"></script>
  6. </head>
  7. <div class="content">
  8. <h2>jQuery Upload Multiple Images In PHP</h2><hr/>
  9. <div id="status"></div>
  10. <div id="mulitplefileuploader">Upload</div>
  11. $(document).ready(function()
  12. {
  13. var settings = {
  14. url: "upload.php",
  15. method: "POST",
  16. allowedTypes:"jpg,png,gif,doc,pdf,zip",
  17. fileName: "myfile",
  18. multiple: true,
  19. onSuccess:function(files,data,xhr)
  20. {
  21. $("#status").html("<font color='green'>Image Upload Successfully</font>");
  22.  
  23. },
  24. afterUploadAll:function()
  25. {
  26. alert("Image's Uploaded");
  27. },
  28. onError: function(files,status,errMsg)
  29. {
  30. $("#status").html("<font color='red'>Image Upload Failed</font>");
  31. }
  32. }
  33. $("#mulitplefileuploader").uploadFile(settings);
  34. });
  35. </div>
  36. <div>
  37. <footer><a href="http://www.sourcecodester.com">Sourcecodester &copy 2016</a></footer>
  38. </div>
  39. </body>
  40. </html>
resultUpload.php - This file is for the process of uploading the images or files and push it through the directory folder.
  1. <?php
  2. $output_dir = "uploads/";
  3.  
  4. if(isset($_FILES["myfile"]))
  5. {
  6. $ret = array();
  7. $error =$_FILES["myfile"]["error"];
  8. {
  9. if(!is_array($_FILES["myfile"]['name']))
  10. {
  11. $RandomNum = time();
  12. $ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
  13. $ImageType = $_FILES['myfile']['type'];
  14. $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
  15. $ImageExt = str_replace('.','',$ImageExt);
  16. $ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
  17. $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
  18.  
  19. move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);
  20. $ret[$fileName]= $output_dir.$NewImageName;
  21. }
  22. else
  23. {
  24. $fileCount = count($_FILES["myfile"]['name']);
  25. for($i=0; $i < $fileCount; $i++)
  26. {
  27. $RandomNum = time();
  28. $ImageName = str_replace(' ','-',strtolower($_FILES['myfile']['name'][$i]));
  29. $ImageType = $_FILES['myfile']['type'][$i];
  30.  
  31. $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
  32. $ImageExt = str_replace('.','',$ImageExt);
  33. $ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
  34. $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
  35.  
  36. $ret[$NewImageName]= $output_dir.$NewImageName;
  37. move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$NewImageName );
  38. }
  39. }
  40. }
  41. echo json_encode($ret);
  42. }
  43. ?>
Hope that you learn in my tutorial. Don't forget to LIKE & SHARE this website. Enjoy Coding.

Add new comment