Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. async function uploadImageAsync(uri) {
  2. let apiUrl = 'upload.php';
  3.  
  4.  
  5.  
  6. let uriParts = uri.split('.');
  7. let fileType = uriParts[uriParts.length - 1];
  8.  
  9. let formData = new FormData();
  10. formData.append('photo', {
  11. uri,
  12. name: `photo.${fileType}`,
  13. type: `image/${fileType}`,
  14. });
  15.  
  16. let options = {
  17. method: 'POST',
  18. body: formData,
  19. headers: {
  20. Accept: 'application/json',
  21. 'Content-Type': 'multipart/form-data',
  22. },
  23. };
  24.  
  25. return fetch(apiUrl, options);
  26. }
  27.  
  28. <input type="file" name="img">
  29.  
  30. if(file_exists($_FILES['img']['tmp_name']) && is_uploaded_file($_FILES['img']['tmp_name']))
  31. {
  32. $target = md5(time());
  33. $target_dir = "photos/";
  34. $target_file = $target.basename($_FILES["img"]["name"]);
  35. $uploadOk = 1;
  36. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  37. // Check if image file is a actual image or fake image
  38. $check = getimagesize($_FILES["img"]["tmp_name"]);
  39. if($check != false) {
  40.  
  41. $uploadOk = 1;
  42. }
  43. else{
  44. echo "File is not an image.";
  45. $uploadOk = 0;
  46. }
  47.  
  48. if ($_FILES["img"]["size"] > 50000000) {
  49. $uploadOk = 0;
  50. echo "Sorry, your file is too large.";
  51. }
  52. // Allow certain file formats
  53. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  54. && $imageFileType != "gif" ) {
  55. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  56. $uploadOk = 0;
  57. }
  58. // Check if $uploadOk is set to 0 by an error
  59. if ($uploadOk == 0) {
  60. echo "Sorry, your image was not uploaded.";
  61. // if everything is ok, try to upload file
  62. } else {
  63.  
  64. $target_file = strtr($target_file1,
  65. 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
  66. 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
  67. $target_file = preg_replace('/([^.a-z0-9]+)/i', '_', $target_file);
  68.  
  69. if (!move_uploaded_file($_FILES["img"]["tmp_name"], $target_dir. $target_file)) {
  70.  
  71. echo "Sorry, there was an error uploading your file. Please retry.";
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement