Advertisement
Whistik

Untitled

Jul 13th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. <?php
  2. include('db-connect.php');
  3.  
  4. if($_SESSION['user_id'] == "") {
  5. echo("user id not found");
  6. }
  7. ?>
  8. <img src="resources/profile_photos/<?=$_SESSION['user_id'];?>.jpg" width="200px" height="200px">
  9. <?php
  10. $target_dir = "resources/profile_photos";
  11. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  12. $uploadOk = 1;
  13. $imageFileType = 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["fileToUpload"]["tmp_name"]);
  17. if($check !== false) {
  18. echo "File is an image - " . $check["mime"] . ".";
  19. $uploadOk = 1;
  20. } else {
  21. echo "File is not an image.";
  22. $uploadOk = 0;
  23. }
  24. }
  25. // Check if file already exists
  26. if (file_exists($target_file)) {
  27. echo "Sorry, file already exists.";
  28. $uploadOk = 0;
  29. }
  30. // Check file size
  31. if ($_FILES["fileToUpload"]["size"] > 500000) {
  32. echo "Sorry, your file is too large.";
  33. $uploadOk = 0;
  34. }
  35. // Allow certain file formats
  36. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
  37. echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  38. $uploadOk = 0;
  39. }
  40. // Check if $uploadOk is set to 0 by an error
  41. if ($uploadOk == 0) {
  42. echo "Sorry, your file was not uploaded.";
  43. // if everything is ok, try to upload file
  44. } else {
  45. if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  46. echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
  47. } else {
  48. echo "Sorry, there was an error uploading your file.";
  49. }
  50. }
  51. ?>
  52. <form action="<?PHP $PHP_SELF; ?>" method="post" enctype="multipart/form-data">
  53. <input type="file" name="fileToUpload" id="fileToUpload">
  54. <input type="submit" value="Upload Image" name="submit">
  55. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement