Guest User

Untitled

a guest
Jan 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. include 'connect.php';
  2.  
  3. if(!empty($_FILES['file'])) {
  4. $allowed = array('jpg', 'gif', 'png', 'jpeg');
  5. $count = 0;
  6. foreach($_FILES['file']['name'] as $key => $name) {
  7. $image_name = $name;
  8. $tmp = explode('.', $image_name);
  9. $image_extn = strtolower(end($tmp)); //can only reference file
  10. $image_temp = $_FILES['file']['tmp_name'][$count];
  11. $count = $count + 1;
  12. if(in_array($image_extn, $allowed) === true) {
  13. $image_path = 'images/'.substr(md5(uniqid($name, true)),0,10).$image_extn;
  14. move_uploaded_file($image_temp, $image_path);
  15. mysql_query("INSERT INTO store VALUES ('','$image_name','$image_path')") or
  16. die(mysql_error());
  17. $lastid = mysql_insert_id();
  18. $image_link = mysql_query("SELECT * FROM store WHERE id = $lastid");
  19. $image_link = mysql_fetch_assoc($image_link);
  20. $image_link = $image_link['image'];
  21. $uploaded[] = $image_link;
  22. }
  23. }
  24. }
  25.  
  26. function showFileSize() {
  27. var input, file, size, total, fileName, ext;
  28. var val=true;
  29. var imageExts = ['png', 'jpg', 'gif', 'jpeg'];
  30. if (typeof window.FileReader !== 'function') {
  31. bodyAppend("p", "The file API isn't supported on this browser yet.");
  32. return;
  33. }
  34.  
  35. input = document.getElementById('file');
  36. if (!input) {
  37. bodyAppend("p", "Um, couldn't find the fileinput element.");
  38. bodyAppend("br","");
  39. }
  40. else if (!input.files) {
  41. bodyAppend("p", "This browser doesn't seem to support the `files` property of file inputs.");
  42. bodyAppend("br","");
  43. }
  44. else if (!input.files[0]) {
  45. bodyAppend("err","Please select a file before clicking 'Load'");
  46. bodyAppend("br","");
  47. }
  48. else if(input.files.length>3){
  49. bodyAppend("err","You can upload a maximum of three files");
  50. bodyAppend("br","");
  51. }
  52. else{
  53. for(var i=0; i<input.files.length; ++i) {
  54. total+=input.files[i].size;
  55. }
  56. if(total>1024*1024*2) {
  57. bodyAppend("err","Total uploaded file sizes must not exceed 2MB");
  58. bodyAppend("br","");
  59. }
  60. else {
  61. for(var i=0; i<input.files.length; ++i) {
  62. file = input.files[i];
  63. size = file.size;
  64. if(size>1024*1024*1) {
  65. bodyAppend("err",file.name+ " is too big, can be a maximum of 1MB");
  66. bodyAppend("br","");
  67. }
  68. fileName = file.name;
  69. ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
  70. if (imageExts.indexOf(ext) === -1) {
  71. bodyAppend("err",file.name+"is an incorrect file type");
  72. bodyAppend("br","");
  73. val = false;
  74. }
  75. }
  76. if(val==false) {
  77. bodyAppend("err","Allowed file types are: jpg, png, jpeg and gif");
  78. bodyAppend("br","");
  79. }
  80. }
  81. }
  82. }
  83.  
  84.  
  85. function bodyAppend(tagName, innerHTML) {
  86. var elm;
  87. elm = document.createElement(tagName);
  88. elm.innerHTML = innerHTML;
  89. document.body.appendChild(elm);
  90. }
  91.  
  92. <form action="index.php"onsubmit="return showFileSize();" method="post" enctype="multipart/form-data">
  93. <div>
  94. <input type="file" id="file" name="file[]" multiple="multiple" />
  95. <input type="submit" id="submit" value="Upload" onclick='showFileSize();'/>
  96. </div>
  97. </form>
Add Comment
Please, Sign In to add comment