ayand04

mystery upload code

Jun 20th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. //PHP
  2.  
  3. <?php
  4. try{
  5. if(isset($_FILES['uploadfile'])){
  6. $total_files = count($_FILES['uploadfile']['name']);
  7. if( $total_files > 0){
  8.  
  9. for($i=0; $i<$total_files; $i++) {
  10.  
  11. $file_name = $_FILES['uploadfile']['name'][$i];
  12. $file_size = $_FILES['uploadfile']['size'][$i];
  13. $file_tmp = $_FILES['uploadfile']['tmp_name'][$i];
  14. $file_type = $_FILES['uploadfile']['type'][$i];
  15.  
  16. $upload_Path = "storage/".$file_name;
  17.  
  18. //var_dump($file_size);
  19. //die;
  20.  
  21. if($file_size > 8000000){
  22. echo json_encode( array('status' => 'failure' , 'msg' => 'Total upload size must be less than 8 MB.') );
  23. die();
  24. }
  25.  
  26. if($file_tmp == ""){
  27. echo json_encode( array('status' => 'failure' , 'msg' => 'There is no filepath.') );
  28. die();
  29. }
  30. else{
  31. if(!file_exists($upload_Path)){
  32. move_uploaded_file($file_tmp, $upload_Path);
  33. }
  34. else{
  35. $name = pathinfo($file_name, PATHINFO_FILENAME);
  36. $ext = pathinfo($file_name, PATHINFO_EXTENSION);
  37. $new_name = $name.rand().'.'.$ext;
  38.  
  39. $new_Path = "storage/".$new_name;
  40. move_uploaded_file($file_tmp, $new_Path);
  41. }
  42. }
  43. }
  44. }
  45. echo json_encode( array('status' => 'success' , 'msg' => 'File uploaded succesfully.') );
  46. die();
  47. }
  48.  
  49. else{
  50.  
  51. echo json_encode(array("status" => "error" , "msg" => "No file was found when processing uploaded files" ) );
  52. die();
  53. }
  54.  
  55.  
  56. }
  57. catch(Exception $ex){
  58.  
  59. echo json_encode(array('status' => 'error' , 'msg' => 'An unhandled exception raised: ' . $ex->getMessage() ) );
  60. die();
  61. }
  62.  
  63. ?>
  64.  
  65.  
  66. //HTML
  67.  
  68. <!doctype html>
  69. <html>
  70. <head>
  71. <meta charset="utf-8">
  72. <title>Upload Files</title>
  73. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  74. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet">
  75. <link href='https://fonts.googleapis.com/css?family=Raleway:400italic|Courgette' rel='stylesheet' type='text/css'>
  76. <link href="styles.css" rel="stylesheet">
  77. <link href="reset.css" rel="stylesheet">
  78. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  79. <script type="text/javascript">
  80. $(document).ready(function(){
  81. $('#upload').click(function(){
  82. $('input[type=file]').click();
  83. return false;
  84. });
  85.  
  86. $("#uploadfile").change(function(){
  87. //submit the form here
  88. var files = $("#fileupload")[0];
  89. var formdata = new FormData(files);
  90. $.ajax({
  91. type:'POST',
  92. url: 'mupld.php',
  93. data: formdata,
  94. processData:false,
  95. contentType:false,
  96. success: function(response){
  97. if(response.status == 'success'){
  98. alert('success: '+response.msg);
  99. }
  100. else{
  101. alert('Error: '+response.msg);
  102. }
  103. }/*,
  104. error: function(xhr, status, error){
  105. alert('Error: '+status+' '+error);
  106. }*/
  107. });
  108. });
  109. });
  110. </script>
  111. </head>
  112.  
  113. <body>
  114. <div id="header">
  115. <a href="./uploader.php"><i class="fa fa-cloud-upload fa-align-center" aria-hidden="true"></i> MUploader</a>
  116. </div>
  117.  
  118. <div id="content">
  119. <div id="heading">Upload your files seamlessly</div>
  120. <a href="#"><div id="upload" class="button" title="Upload your files"><i class="fa fa-cloud-upload fa-align-center" aria-hidden="true"></i>
  121. </div></a>
  122. <a href="view.php"><div id="view" class="button" title="View all files on my cloud"><i class="fa fa-eye fa-align-center" aria-hidden="true"></i>
  123. </div></a>
  124. </div>
  125. <form id="fileupload" method="POST" enctype="multipart/form-data">
  126. <input type="file" multiple name="uploadfile[]" id="uploadfile" />
  127. </form>
  128. <div id="footer">
  129. Made with <span class="fa fa-heart" aria-hidden="true"></span> for you
  130. </div>
  131. </body>
  132. </html>
Add Comment
Please, Sign In to add comment