Guest User

Untitled

a guest
Jul 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. <form name="photo" enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
  2. Photo <input type="file" name="image" size="30" />
  3. <input type="submit" name="upload" value="Upload" />
  4. </form>
  5.  
  6. if (isset($_POST["upload"])) {
  7. //Get the file information
  8. $userfile_name = $_FILES['image']['name'];
  9. $userfile_tmp = $_FILES['image']['tmp_name'];
  10. $userfile_size = $_FILES['image']['size'];
  11. $userfile_type = $_FILES['image']['type'];
  12. $filename = basename($_FILES['image']['name']);
  13. $file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
  14.  
  15. //Only process if the file is a JPG, PNG or GIF and below the allowed limit
  16. if((!empty($_FILES["image"])) && ($_FILES['image']['error'] == 0)) {
  17.  
  18. foreach ($allowed_image_types as $mime_type => $ext) {
  19. //loop through the specified image types and if they match the extension then break out
  20. //everything is ok so go and check file size
  21. if($file_ext==$ext && $userfile_type==$mime_type){
  22. $error = "";
  23. break;
  24. }else{
  25. $error = "Only <strong>".$image_ext."</strong> images accepted for upload<br />";
  26. }
  27. }
  28. //check if the file size is above the allowed limit
  29. if ($userfile_size > ($max_file*1048576)) {
  30. $error.= "Images must be under ".$max_file."MB in size";
  31. }
  32.  
  33. }else{
  34. $error= "Select an image for upload";
  35. }
  36. //Everything is ok, so we can upload the image.
  37. if (strlen($error)==0){
  38.  
  39. if (isset($_FILES['image']['name'])){
  40. //this file could now has an unknown file extension (we hope it's one of the ones set above!)
  41. $large_image_location = $large_image_location.".".$file_ext;
  42. $thumb_image_location = $thumb_image_location.".".$file_ext;
  43.  
  44. //put the file ext in the session so we know what file to look for once its uploaded
  45. $_SESSION['user_file_ext']=".".$file_ext;
  46.  
  47. move_uploaded_file($userfile_tmp, $large_image_location);
  48. chmod($large_image_location, 0777);
  49.  
  50. $width = getWidth($large_image_location);
  51. $height = getHeight($large_image_location);
  52. //Scale the image if it is greater than the width set above
  53. if ($width > $max_width){
  54. $scale = $max_width/$width;
  55. $uploaded = resizeImage($large_image_location,$width,$height,$scale);
  56. }else{
  57. $scale = 1;
  58. $uploaded = resizeImage($large_image_location,$width,$height,$scale);
  59. }
  60. //Delete the thumbnail file so the user can create a new one
  61. if (file_exists($thumb_image_location)) {
  62. unlink($thumb_image_location);
  63. }
  64. }
  65. //Refresh the page to show the new uploaded image
  66. header("location:".$_SERVER["PHP_SELF"]);
  67. exit();
  68. }
  69. }
  70.  
  71. $('#uploadBtn').live('click', function(){
  72. var data = "upload=true";
  73. $.ajax({
  74. type: "POST",
  75. url: "process.php",
  76. data: data,
  77. cache: false,
  78. success: function(data){
  79. }
  80. });
  81. });
Add Comment
Please, Sign In to add comment