Browsing articles from "May, 2009"

A Simple PHP Multi Uploader with rand() and time()

May 17, 2009   //   by nuhil   //   MySQL, PHP, Tips for the New  //  1 Comment

I think this would be the simplest file multi uploader to an specific location. It uses copy() for the upload method and add a random number based on time to avoid replacement of the files including same file name.

This is the approach:

The form for uploading:

  1. <table border="0" cellspacing="1" cellpadding="0" width="500" align="center" bgcolor="#cccccc">
  2. <tbody>
  3. <tr>
  4. <form id="form1" action="multiple_upload_ac.php" enctype="multipart/form-data" method="post">
  5. <td>
  6. <table border="0" cellspacing="1" cellpadding="3" width="100%" bgcolor="#ffffff">
  7. <tbody>
  8. <tr>
  9. <td><strong>multiple Files Upload </strong></td>
  10. </tr>
  11. <tr>
  12. <td>Select file
  13. <input id="ufile[]" name="ufile[]" size="50" type="file" /></td>
  14. </tr>
  15. <tr>
  16. <td>Select file
  17. <input id="ufile[]" name="ufile[]" size="50" type="file" /></td>
  18. </tr>
  19. <tr>
  20. <td>Select file
  21. <input id="ufile[]" name="ufile[]" size="50" type="file" /></td>
  22. </tr>
  23. <tr>
  24. <td align="center">
  25. <input name="Submit" type="submit" value="Upload" /></td>
  26. </tr>
  27. </tbody></table>
  28. </td>
  29. </form></tr>
  30. </tbody></table>

multi

The processor file:

“);
  1. $path1=$path1a.time().$num1.'_'.basename($_FILES['ufile']['name'][0]);
  2.  
  3. //add a random name with the base name
  4. $path2=$path2a.time().$num2.'_'.basename($_FILES['ufile']['name'][1]);
  5. $path3=$path3a.time().$num3.'_'.basename($_FILES['ufile']['name'][2]);
  6.  
  7. //copy file to where you want to store file
  8. copy($_FILES['ufile']['tmp_name'][0], $path1);
  9. copy($_FILES['ufile']['tmp_name'][1], $path2);
  10. copy($_FILES['ufile']['tmp_name'][2], $path3);
  11.  
  12. //$_FILES['ufile']['name'] = file name
  13. //$_FILES['ufile']['size'] = file size
  14. //$_FILES['ufile']['type'] = type of file
  15. echo "File Name :".$_FILES['ufile']['name'][0]."
  16. ";
  17. echo "File Size :".$_FILES['ufile']['size'][0]."
  18. ";
  19. echo "File Type :".$_FILES['ufile']['type'][0]."
  20. ";
  21. echo "<img src="\" alt="" width="150" height="150" />";
  22. echo "
  23. ";
  24.  
  25. echo "File Name :".$_FILES['ufile']['name'][1]."
  26. ";
  27. echo "File Size :".$_FILES['ufile']['size'][1]."
  28. ";
  29. echo "File Type :".$_FILES['ufile']['type'][1]."
  30. ";
  31. echo "<img src="\" alt="" width="150" height="150" />";
  32. echo "
  33.  
  34. ";
  35.  
  36. echo "File Name :".$_FILES['ufile']['name'][2]."
  37. ";
  38. echo "File Size :".$_FILES['ufile']['size'][2]."
  39. ";
  40. echo "File Type :".$_FILES['ufile']['type'][2]."
  41. ";
  42. echo "<img src="\" alt="" width="150" height="150" />";
  43.  
  44. ///////////////////////////////////////////////////////
  45.  
  46. // Use this code to display the error or success.
  47.  
  48. $filesize1=$_FILES['ufile']['size'][0];
  49. $filesize2=$_FILES['ufile']['size'][1];
  50. $filesize3=$_FILES['ufile']['size'][2];
  51.  
  52. if($filesize1 &amp;&amp; $filesize2 &amp;&amp; $filesize3 != 0)
  53. {
  54. echo "We have recieved your files";
  55. }
  56.  
  57. else {
  58. echo "ERROR…..";
  59. }
  60.  
  61. //////////////////////////////////////////////
  62.  
  63. // What files that have a problem? (if found)
  64.  
  65. if($filesize1==0) {
  66. echo "There're something error in your first file";
  67. echo "
  68. ";
  69. }
  70.  
  71. if($filesize2==0) {
  72. echo "There're something error in your second file";
  73. echo "
  74. ";
  75. }
  76.  
  77. if($filesize3==0) {
  78. echo "There're something error in your third file";
  79. echo "
  80. ";
  81. }
  82.  
  83. ?&gt;

Download the code from left flush widget “upload.zip”