Guest User

Untitled

a guest
Sep 4th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. <form action="includes/profile_picture_inc.php" method="POST" enctype="multipart/form-data" id="form">
  2. <input type="file" name="file" id="upload" onchange="document.getElementById('form').submit();">
  3. </form>
  4.  
  5. if (isset($_POST['submit'])) {
  6. $target_dir = "../users/".$_SESSION['u_id']."/image/";
  7. $str = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
  8. $rand = substr(str_shuffle($str), 0, 10);
  9. $temp = explode(".", $_FILES["file"]["name"]);
  10. $newfilename = $rand . '.' . end($temp);
  11. $target_file = $target_dir . $newfilename;
  12. $uploadOk = 1;
  13. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  14. // Check if image file is a actual image or fake image
  15. if(isset($_POST["submit"])) {
  16. $check = getimagesize($_FILES["file"]["tmp_name"]);
  17. if($check !== false) {
  18. echo "File is an image - " . $check["mime"] . ". <br/>";
  19. $uploadOk = 1;
  20. } else {
  21. echo "File is not an image. <br/>";
  22. $uploadOk = 0;
  23. }
  24. }
  25. // Check if file already exists
  26. if (file_exists($target_file)) {
  27. echo "Sorry, file already exists. <br/>";
  28. $uploadOk = 0;
  29. }
  30. // Check file size
  31. if ($_FILES["file"]["size"] > 5000000) {
  32. echo "Sorry, your file is too large. <br/>";
  33. $uploadOk = 0;
  34. }
  35. // Allow certain file formats
  36. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
  37. && $imageFileType != "gif" ) {
  38. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed. <br/>";
  39. $uploadOk = 0;
  40. }
  41. // Check if $uploadOk is set to 0 by an error
  42. if ($uploadOk == 0) {
  43. echo "Sorry, your file was not uploaded. <br/>";
  44. // if everything is ok, try to upload file
  45. } else {
  46. if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
  47. require 'database_inc.php';
  48. $sql = "UPDATE `users` SET `user_profile` = '$newfilename' WHERE 1";
  49. if (mysqli_query($conn,$sql)) {
  50. $_SESSION['profile_picture'] = $newfilename;
  51. header("Location: ../profile.php");
  52. } else {
  53. echo "The query has not been updated. <br/>";
  54. }
  55.  
  56. } else {
  57. echo "Sorry, there was an error uploading your file. <br/>";
  58. }
  59. }
  60. }
  61.  
  62. if (isset($_POST['submit'])) {
  63.  
  64. <form action="includes/profile_picture_inc.php" method="POST" enctype="multipart/form-data" id="form">
  65. <input type="file" name="file" id="upload" onchange="document.getElementById('form').submit();">
  66. <input type="hidden" name="submit" value="1" />
  67. </form>
Add Comment
Please, Sign In to add comment