Guest User

Untitled

a guest
Feb 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. <input id="sortpicture" type="file" name="sortpic" />
  2. <button id="upload">Upload</button>
  3.  
  4. $("#upload").on("click", function() {
  5. var file_data = $("#sortpicture").prop("files")[0];
  6. var form_data = new FormData();
  7. form_data.append("file", file_data);
  8. alert(form_data);
  9. $.ajax({
  10. url: "/uploads",
  11. dataType: 'script',
  12. cache: false,
  13. contentType: false,
  14. processData: false,
  15. data: form_data,
  16. type: 'post',
  17. success: function(){
  18. alert("works");
  19. }
  20. });
  21. });
  22.  
  23. $('#upload').on('click', function() {
  24. var file_data = $('#sortpicture').prop('files')[0];
  25. var form_data = new FormData();
  26. form_data.append('file', file_data);
  27. alert(form_data);
  28. $.ajax({
  29. url: 'upload.php', // point to server-side PHP script
  30. dataType: 'text', // what to expect back from the PHP script, if anything
  31. cache: false,
  32. contentType: false,
  33. processData: false,
  34. data: form_data,
  35. type: 'post',
  36. success: function(php_script_response){
  37. alert(php_script_response); // display response from the PHP script, if any
  38. }
  39. });
  40. });
  41.  
  42. <?php
  43.  
  44. if ( 0 < $_FILES['file']['error'] ) {
  45. echo 'Error: ' . $_FILES['file']['error'] . '<br>';
  46. }
  47. else {
  48. move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
  49. }
  50.  
  51. ?>
  52.  
  53. move_uploaded_file(
  54.  
  55. // this is where the file is temporarily stored on the server when uploaded
  56. // do not change this
  57. $_FILES['file']['tmp_name'],
  58.  
  59. // this is where you want to put the file and what you want to name it
  60. // in this case we are putting in a directory called "uploads"
  61. // and giving it the original filename
  62. 'uploads/' . $_FILES['file']['name']
  63. );
  64.  
  65. move_uploaded_file(
  66. $_FILES['file']['tmp_name'],
  67. 'uploads/my_new_filename.whatever'
  68. );
  69.  
  70. var formData = new FormData($("#YOUR_FORM_ID")[0]);
  71. $.ajax({
  72. url: "upload.php",
  73. type: "POST",
  74. data : formData,
  75. processData: false,
  76. contentType: false,
  77. beforeSend: function() {
  78.  
  79. },
  80. success: function(data){
  81.  
  82.  
  83.  
  84.  
  85. },
  86. error: function(xhr, ajaxOptions, thrownError) {
  87. console.log(thrownError + "rn" + xhr.statusText + "rn" + xhr.responseText);
  88. }
  89. });
  90.  
  91. <?
  92. $data = array();
  93. //check with your logic
  94. if (isset($_FILES)) {
  95. $error = false;
  96. $files = array();
  97.  
  98. $uploaddir = $target_dir;
  99. foreach ($_FILES as $file) {
  100. if (move_uploaded_file($file['tmp_name'], $uploaddir . basename( $file['name']))) {
  101. $files[] = $uploaddir . $file['name'];
  102. } else {
  103. $error = true;
  104. }
  105. }
  106. $data = ($error) ? array('error' => 'There was an error uploading your files') : array('files' => $files);
  107. } else {
  108. $data = array('success' => 'NO FILES ARE SENT','formData' => $_REQUEST);
  109. }
  110.  
  111. echo json_encode($data);
  112. ?>
  113.  
  114. **1. index.php**
  115. <body>
  116. <span id="msg" style="color:red"></span><br/>
  117. <input type="file" id="photo"><br/>
  118. <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
  119. <script type="text/javascript">
  120. $(document).ready(function(){
  121. $(document).on('change','#photo',function(){
  122. var property = document.getElementById('photo').files[0];
  123. var image_name = property.name;
  124. var image_extension = image_name.split('.').pop().toLowerCase();
  125.  
  126. if(jQuery.inArray(image_extension,['gif','jpg','jpeg','']) == -1){
  127. alert("Invalid image file");
  128. }
  129.  
  130. var form_data = new FormData();
  131. form_data.append("file",property);
  132. $.ajax({
  133. url:'upload.php',
  134. method:'POST',
  135. data:form_data,
  136. contentType:false,
  137. cache:false,
  138. processData:false,
  139. beforeSend:function(){
  140. $('#msg').html('Loading......');
  141. },
  142. success:function(data){
  143. console.log(data);
  144. $('#msg').html(data);
  145. }
  146. });
  147. });
  148. });
  149. </script>
  150. </body>
  151.  
  152. **2.upload.php**
  153. <?php
  154. if($_FILES['file']['name'] != ''){
  155. $test = explode('.', $_FILES['file']['name']);
  156. $extension = end($test);
  157. $name = rand(100,999).'.'.$extension;
  158.  
  159. $location = 'uploads/'.$name;
  160. move_uploaded_file($_FILES['file']['tmp_name'], $location);
  161.  
  162. echo '<img src="'.$location.'" height="100" width="100" />';
  163. }
Add Comment
Please, Sign In to add comment