Advertisement
Guest User

unamed

a guest
Mar 5th, 2020
170
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <form action="" method="post" enctype="multipart/form-data">
  2. <input type="file" name="myFile"/>
  3. <input type="submit" value="Upload"/>
  4. </form>
  5.  
  6. <?php
  7. define("UPLOAD_DIR", "images/");
  8.  
  9. if (!empty($_FILES['myFile']['name'])) {
  10.  
  11. $fileType = exif_imagetype($_FILES['myFile']['tmp_name']);
  12. $allowed = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
  13. if (!in_array($fileType, $allowed)) {
  14. $result = array('success' => 'false',
  15. 'result' => 'hanya diijinkan untuk meng-upload file gambar (gif, jpg, atau png)');
  16. echo json_encode($result);
  17. }
  18.  
  19. else{
  20. $myFile = $_FILES['myFile'];
  21. $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile['name']);
  22. $parts = pathinfo($name);
  23. $res = move_uploaded_file($myFile['tmp_name'], UPLOAD_DIR . $name);
  24. $result = array('success' => 'true',
  25. 'result' => "images/" . $_FILES['myFile']['name']);
  26. echo json_encode($result);
  27. }
  28. }
Advertisement
RAW Paste Data Copied
Advertisement