Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. <form method="post" name="update_form" action="php/editpost" enctype="multipart/form-data">
  2.  
  3. <div class="form-group">
  4. <label for="img_upload">Select image</label>
  5. <input type="file" name="img_upload" id="img_upload" class="form-control">
  6. </div>
  7.  
  8. <div class="form-group">
  9. <input class="btn btn-success btn-lg" type="submit" value="Update" name="edit_submit" id="edit_submit">
  10. </div>
  11.  
  12. </form>
  13.  
  14. if ($_FILES["img_upload"]["name"]) {
  15.  
  16. $target_dir = "uploads/";
  17. $target_file = $target_dir . basename($_FILES["img_upload"]["name"]);
  18. $imgsrc = $target_file;
  19. $uploadOk = 1;
  20. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  21.  
  22. // Check if image file is a actual image and square
  23. $check = getimagesize($_FILES["img_upload"]["tmp_name"]);
  24. if($check !== false) {
  25. $width = $check[0];
  26. $height = $check[1];
  27. if ($width != $height) {
  28. $error = $error."<br />- The image is not square, please crop it and try again.";
  29. $uploadOk = 0;
  30. }
  31. } else {
  32. $error = $error."<br />- File is not an image.";
  33. $uploadOk = 0;
  34. }
  35.  
  36. // Check if file already exists
  37. if (file_exists($target_file)) {
  38. chmod($target_file,0755); // Change the file permissions
  39. unlink($target_file); // remove the file
  40. }
  41.  
  42. // Check file size
  43. if ($_FILES["img_upload"]["size"] > 1000000) {
  44. $error = $error."<br />- Your image is too large.";
  45. $uploadOk = 0;
  46. }
  47.  
  48. // Allow certain file formats
  49. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
  50. $error = $error."<br>- Only JPG, JPEG & PNG images are allowed.";
  51. $uploadOk = 0;
  52. }
  53.  
  54. // Check if $uploadOk is set to 0 by an error
  55. if ($uploadOk == 0) {
  56. $fail .= "There were error(s) while uploading the image:<br />".$error;
  57. }
  58.  
  59. // if everything is ok, try to upload file
  60. else {
  61.  
  62. if (move_uploaded_file($_FILES["img_upload"]["tmp_name"], $target_file)) {
  63.  
  64. $sql = mysqli_query($link,"UPDATE `posts` SET `imagesrc`='$imgsrc' WHERE `postid`='$postid' LIMIT 1");
  65.  
  66. if ($sql) {
  67. $success .= '- Image updated successfully.<br />';
  68. } else {
  69. $fail .= "- Failed to update the image.<br />";
  70. }
  71. }
  72. }
  73. }
  74.  
  75. header("location:../admincontrol?result_s=".urlencode($success)."&result_f=".urlencode($fail));
  76. exit();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement