mult.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Multi file upload example
  4. * @author Resalat Haque
  5. * @link http://www.w3bees.com/2013/02/multiple-file-upload-with-php.html
  6. **/
  7. $valid_formats = array("jpg", "png", "gif", "zip", "bmp");
  8. $max_file_size = 1024*5000; //100 kb
  9. $path = "upload/"; // Upload directory
  10. $count = 0;
  11. if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
  12. // Loop $_FILES to execute all files
  13. foreach ($_FILES['files']['name'] as $f => $name) {
  14. if ($_FILES['files']['error'][$f] == 4) {
  15. continue; // Skip file if any error found
  16. }
  17. if ($_FILES['files']['error'][$f] == 0) {
  18. if ($_FILES['files']['size'][$f] > $max_file_size) {
  19. $message[] = "$name is too large!.";
  20. continue; // Skip large files
  21. }
  22. elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
  23. $message[] = "$name is not a valid format";
  24. continue; // Skip invalid file formats
  25. }
  26. else{ // No error found! Move uploaded files
  27. if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name)) {
  28. $count++; // Number of successfully uploaded files
  29. }
  30. }
  31. }
  32. }
  33. }
  34. ?>
  35. <!doctype html>
  36. <html lang="en">
  37. <head>
  38. <meta charset="UTF-8" />
  39. <title>Multiple File Upload with PHP - Demo</title>
  40. <style type="text/css">
  41. a{ text-decoration: none; color: #333}
  42. h1{ font-size: 1.9em; margin: 10px 0}
  43. p{ margin: 8px 0}
  44. *{
  45. margin: 0;
  46. padding: 0;
  47. box-sizing: border-box;
  48. -webkit-box-sizing: border-box;
  49. -moz-box-sizing: border-box;
  50. -webkit-font-smoothing: antialiased;
  51. -moz-font-smoothing: antialiased;
  52. -o-font-smoothing: antialiased;
  53. font-smoothing: antialiased;
  54. text-rendering: optimizeLegibility;
  55. }
  56. body{
  57. font: 12px Arial,Tahoma,Helvetica,FreeSans,sans-serif;
  58. text-transform: inherit;
  59. color: #333;
  60. background: #e7edee;
  61. width: 100%;
  62. line-height: 18px;
  63. }
  64. .wrap{
  65. width: 500px;
  66. margin: 15px auto;
  67. padding: 20px 25px;
  68. background: white;
  69. border: 2px solid #DBDBDB;
  70. -webkit-border-radius: 5px;
  71. -moz-border-radius: 5px;
  72. border-radius: 5px;
  73. overflow: hidden;
  74. text-align: center;
  75. }
  76. .status{
  77. /*display: none;*/
  78. padding: 8px 35px 8px 14px;
  79. margin: 20px 0;
  80. text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  81. color: #468847;
  82. background-color: #dff0d8;
  83. border-color: #d6e9c6;
  84. -webkit-border-radius: 4px;
  85. -moz-border-radius: 4px;
  86. border-radius: 4px;
  87. }
  88. input[type="submit"] {
  89. cursor:pointer;
  90. width:100%;
  91. border:none;
  92. background:#991D57;
  93. background-image:linear-gradient(bottom, #8C1C50 0%, #991D57 52%);
  94. background-image:-moz-linear-gradient(bottom, #8C1C50 0%, #991D57 52%);
  95. background-image:-webkit-linear-gradient(bottom, #8C1C50 0%, #991D57 52%);
  96. color:#FFF;
  97. font-weight: bold;
  98. margin: 20px 0;
  99. padding: 10px;
  100. border-radius:5px;
  101. }
  102. input[type="submit"]:hover {
  103. background-image:linear-gradient(bottom, #9C215A 0%, #A82767 52%);
  104. background-image:-moz-linear-gradient(bottom, #9C215A 0%, #A82767 52%);
  105. background-image:-webkit-linear-gradient(bottom, #9C215A 0%, #A82767 52%);
  106. -webkit-transition:background 0.3s ease-in-out;
  107. -moz-transition:background 0.3s ease-in-out;
  108. transition:background-color 0.3s ease-in-out;
  109. }
  110. input[type="submit"]:active {
  111. box-shadow:inset 0 1px 3px rgba(0,0,0,0.5);
  112. }
  113. </style>
  114. </head>
  115. <body>
  116. <div class="wrap">
  117. <h1><a href="http://www.w3bees.com/2013/02/multiple-file-upload-with-php.html">Multiple File Upload with PHP</a></h1>
  118. <?php
  119. # error messages
  120. if (isset($message)) {
  121. foreach ($message as $msg) {
  122. printf("<p class='status'>%s</p></ br>\n", $msg);
  123. }
  124. }
  125. # success message
  126. if($count !=0){
  127. printf("<p class='status'>%d files added successfully!</p>\n", $count);
  128. }
  129. ?>
  130. <p>Max file size 100kb, Valid formats jpg, png, gif</p>
  131. <br />
  132. <br />
  133. <!-- Multiple file upload html form-->
  134. <form action="" method="post" enctype="multipart/form-data">
  135. <input type="file" name="files[]" multiple="multiple" accept="image/*">
  136. <input type="submit" value="Upload">
  137. </form>
  138. </div>
  139. </body>
  140. </html>